【问题标题】:"Access_Denied" for Google AnalyticsGoogle Analytics 的“Access_Denied”
【发布时间】:2015-07-21 16:35:42
【问题描述】:

我正在我的 asp.net webapi 2 中实现 oAuth 流。我必须获得用户的许可才能访问他们的谷歌分析数据。为此,我在向 google oAuth2.0 流发送身份验证请求时添加了以下范围。作为对此的回应,即使用户单击同意屏幕上的“允许访问”按钮,我也总是收到“Access_Denied”错误。

如果我不在范围内添加 Google Analytic,相同的代码可以正常工作并获取外部访问令牌。

我还在谷歌开发者控制台中启用了谷歌分析 API。

谷歌分析范围:

googleAuthOptions.Scope.Add(AnalyticsService.Scope.Analytics);
googleAuthOptions.Scope.Add(AnalyticsService.Scope.AnalyticsReadonly);
googleAuthOptions.Scope.Add(AnalyticsService.Scope.AnalyticsManageUsers);

编写在WebApi的startup.cs文件中的代码:

//Configure Google External Login
GoogleOAuth2AuthenticationOptions googleAuthOptions = new GoogleOAuth2AuthenticationOptions()
{
    ClientId = ConfigurationManager.AppSettings["GoogleClientID"],
    ClientSecret = ConfigurationManager.AppSettings["GoogleClientSecret"],
    Provider = new GoogleAuthProvider()
};

//1st trial=> 
//googleAuthOptions.Scope.Add("https://www.googleapis.com/auth/analytics");

//2nd trial
//googleAuthOptions.Scope.Add(AnalyticsService.Scope.Analytics);
//googleAuthOptions.Scope.Add(AnalyticsService.Scope.AnalyticsReadonly);            
//googleAuthOptions.Scope.Add(AnalyticsService.Scope.AnalyticsManageUsers);

app.UseGoogleAuthentication(googleAuthOptions);

WebAPI 控制器 [ExternalLogin] 中的方法:

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

        if (error != null)
        {
            return BadRequest(Uri.EscapeDataString(error));
        }

        if (!User.Identity.IsAuthenticated)
        {
            return new ChallengeResult(provider, this);
        }

        var redirectUriValidationResult = ValidateClientAndRedirectUri(this.Request, ref redirectUri);

        if (!string.IsNullOrWhiteSpace(redirectUriValidationResult))
        {
            return BadRequest(redirectUriValidationResult);
        }

        ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

        if (externalLogin == null)
        {
            return InternalServerError();
        }

        if (externalLogin.LoginProvider != provider)
        {
            Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            return new ChallengeResult(provider, this);
        }

        //IdentityUser user = await _repo.FindAsync(new UserLoginInfo(externalLogin.LoginProvider, externalLogin.ProviderKey));

        int profileID = Convert.ToInt32(HttpContext.Current.Request.QueryString["profile_id"]);

        var user = new UserRepository().SearchProfileByID(profileID);

        bool hasRegistered = user != null;

        redirectUri = string.Format("{0}#external_access_token={1}&provider={2}&haslocalaccount={3}&external_user_name={4}",
                                        redirectUri,
                                        externalLogin.ExternalAccessToken,
                                        externalLogin.LoginProvider,
                                        hasRegistered.ToString(),
                                        externalLogin.UserName);

        return Redirect(redirectUri);

    }

我已经按照this 链接实现了 oAuth 功能。

请帮我解决这个问题。

谢谢

【问题讨论】:

  • 您需要发布所有不足以调试问题的代码。您在使用 Google 的客户端库吗?
  • 您好 DalmTo,感谢您的回复。我已经用代码和链接更新了我的原始问题。如果您想了解更多详情,请告诉我。
  • 请帮我解决这个问题。我仍然无法解决此问题
  • 我仍然想知道你为什么不使用 Google 的客户端库
  • 这里有一个 MVC 示例示例项目,只是尚未合并到主项目中。 github.com/google/google-api-dotnet-client-samples/pulls

标签: asp.net-mvc asp.net-web-api google-analytics google-analytics-api access-denied


【解决方案1】:

终于我的问题得到了解决,我们还需要添加“电子邮件”范围。 我添加了以下 2 个范围以使其正常工作:

googleAuthOptions.Scope.Add("email");  
googleAuthOptions.Scope.Add("https://www.googleapis.com/auth/analytics.readonly");

这现在通过谷歌分析 api 访问返回给我的外部令牌

谢谢

维杰

【讨论】:

    猜你喜欢
    • 2015-07-25
    • 1970-01-01
    • 2012-12-02
    • 2018-10-21
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多