【问题标题】:Google Analytics throws 403 error谷歌分析抛出 403 错误
【发布时间】:2013-07-18 04:24:00
【问题描述】:

我正在尝试使用 C# 从 Google Analytics 下载指标数据,并正在使用 OAuth 2.0 执行用户身份验证。我正在使用 Installed Application 授权流程,这需要登录 Google 并将代码复制并粘贴到应用程序中。我正在关注来自google-api-dotnet-client的代码:

private void DownloadData()
{
    Service = new AnalyticsService(new BaseClientService.Initializer() {
        Authenticator = CreateAuthenticator(),
    });
    var request = service.Data.Ga.Get(AccountID, StartDate, EndDate, Metrics);
    request.Dimensions = Dimensions;
    request.StartIndex = 1;
    request.MaxResults = 10000;
    var response = request.Execute(); // throws Google.GoogleApiException
}

private IAuthenticator CreateAuthenticator()
{
    var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description) {
        ClientIdentifier = "123456789012.apps.googleusercontent.com",
        ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxx",
    };
    return new OAuth2Authenticator<NativeApplicationClient>(provider, Login);
}

private static IAuthorizationState Login(NativeApplicationClient arg)
{
    // Generate the authorization URL.
    IAuthorizationState state = new AuthorizationState(new[] { AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue() });
    state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
    Uri authUri = arg.RequestUserAuthorization(state);

    // Request authorization from the user by opening a browser window.
    Process.Start(authUri.ToString());
    Console.Write("Google Authorization Code: ");
    string authCode = Console.ReadLine();

    // Retrieve the access token by using the authorization code.
    state = arg.ProcessUserAuthorization(authCode, state);
    return state;
}

Google 帐户 xxxxxx@gmail.com 注册了客户端 ID 和密码。同一帐户在 Google Analytics 中拥有完全的管理权限。当我尝试从 Google Analytics 中提取数据时,它会通过授权过程,这似乎可以正常工作。然后它失败了:

Google.GoogleApiException
Google.Apis.Requests.RequestError
用户对此配置文件没有足够的权限。 [403]
错误 [
消息[用户对此配置文件没有足够的权限。] 位置[ - ] 原因 [insufficientPermissions] 域[全局]
]

我已经为此苦苦挣扎了几个小时。我已经仔细检查了是否使用了正确的用户,并且在 Google Analytics 上获得了授权。我不知道什么配置错误。关于需要配置或更改什么的任何想法?

【问题讨论】:

    标签: c# google-analytics google-oauth


    【解决方案1】:

    如果 auth 似乎工作正常,那么我的建议是您确保提供正确的 ID,因为基于您的代码 sn-p:

    var request = service.Data.Ga.Get(AccountID, StartDate, EndDate, Metrics);
    

    只能假设您使用的是帐户 ID。如果是这样,那是不正确的,您会收到遇到的错误。您需要使用 Profile ID 进行查询。

    如果您使用网络界面登录 Google Analytics,您将在浏览器地址栏的 URL 中看到以下模式:

    /a12345w654321p9876543/
    

    p 后面的数字是配置文件 ID,因此在上面的示例中为 9876543。确保您正在使用它,实际上您应该使用table id,即 ga:9876543。

    如果不是 ID 问题,则改为查询 Management API 以列出帐户并查看您有权访问的内容并验证身份验证是否正常工作。

    【讨论】:

    • 非常感谢!如果 Google Analytics(分析)管理 UI 在某处宣传这个数字,那就太好了……
    • 小崽子,我生命中的五个小时现在已经过去了,再也不会回来了。 Google 的文档中几乎没有任何内容表明该值存在于帐户 ID 之外,并且没有被任何其他机制公开。非常感谢你分享这个。
    • 太棒了!在尝试让 Google API 与 .net 中的分析一起工作 2 天后,我刚刚跨过最后一道障碍,这最后一步对我帮助很大。
    【解决方案2】:

    【讨论】:

      【解决方案3】:

      //感谢这篇文章。可以从帐户摘要中读取所需的配置文件 ID。

      字典配置文件 = new Dictionary();

              var accounts = service.Management.AccountSummaries.List().Execute();
              foreach (var account in accounts.Items)
              {
                  var profileId = account.WebProperties[0].Profiles[0].Id;
      
                  profiles.Add("ga:" + profileId, account.Name);
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-18
        • 1970-01-01
        • 2016-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-14
        • 1970-01-01
        相关资源
        最近更新 更多