繁体   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