簡體   English   中英

來自web api的Google身份驗證:access_denied錯誤

[英]Google authentication from web api: access_denied error

我正在嘗試對WebApi實施OAuth身份驗證,我已經使用方法創建了控制器(直接來自示例):

    [OverrideAuthentication]
    [HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
    [AllowAnonymous]
    [Route("ExternalLogin", Name = "ExternalLogin")]
    public IHttpActionResult GetExternalLogin(string provider, string error = null)
    {
        string redirectUri = string.Empty;

        if (error != null)
        {
            // However google api returns 'access_denied' as error.
            return BadRequest(Uri.EscapeDataString(error));
        }

        if (!User.Identity.IsAuthenticated)
        {
            // This is runned on first execution.
            return new ChallengeResult(provider, this);
        }

        // Here we should continue with google api callback.
        ... Rest doesnt matter here.

ChallengeResult:

public class ChallengeResult : IHttpActionResult
{
    public string LoginProvider { get; set; }
    public HttpRequestMessage Request { get; set; }

    public ChallengeResult(string loginProvider, ApiController controller)
    {
        LoginProvider = loginProvider;
        Request = controller.Request;
    }

    public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
    {
        Request.GetOwinContext().Authentication.Challenge(LoginProvider);

        var response = new HttpResponseMessage(HttpStatusCode.Unauthorized);
        response.RequestMessage = Request;
        return Task.FromResult(response);
    }
}

GetExternalLogin方法被調用兩次,首先是來自我,之后api發送ChallengeResult到google。 我被重定向到谷歌網站,並詢問有效范圍的問題(我可以訪問。例如電子郵件,個人資料信息等),我按是是的一切都對我好。 但是之后谷歌回調會向此方法返回'access_denied'錯誤字符串。

知道什么可能是錯的嗎? 我使用的電話是:

http://localhost:8080/api/Account/ExternalLogin?provider=Google&response_type=token&client_id=49235566333-78t8252p46lo75j5e52vda3o1t8fskgc.apps.googleusercontent.com&redirect_uri=http://localhost:8080

Client_id被虛擬帳戶替換。

redirect_uri正確定義為谷歌控制台,如果錯誤,則錯誤不同。

嘗試: 使用Google+ for Domains列出圈子API在access_denied中失敗,但ID:s是相同的。

編輯:經過幾個小時的調試已經發現我的解決方案和示例之間的問題是Microsoft.Owing.Security.Google包。 在示例中使用的版本是2.1.0,如果我將其更新為3.0.0,則會出現此問題。

不知道根本原因。

我也有這個問題。 要解決此問題,請嘗試修改您的Google應用以使用Google + API。 我之前只使用過“Identity Toolkit API”。 根據Pranav指出的文章,當您升級到Google Middleware 3.0.0(Microsoft.Owin.Security.Google)時,您需要使用Google + API。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM