【问题标题】:C# Google Analytics UnsampledReports.Insert returns 'Insufficient Permission [403]'C# Google Analytics UnsampledReports.Insert 返回“权限不足 [403]”
【发布时间】:2014-10-29 15:18:27
【问题描述】:

我的 C# 程序成功调用了我拥有的 Google Premium 帐户的 Analytics Core API 和 Management API(通过代理设置)。

我的“AnalyticsService 服务”成功调用的示例如下:

service.Management.Accounts.List();
service.Management.Webproperties.List(account.Id);
service.Management.Profiles.List(account.Id, webproperty.Id);
service.Data.Ga.Get("ga:" + profile_id, end_date.ToString("yyyy-MM-dd"), end_date.AddDays(1).ToString("yyyy-MM-dd"), "ga:uniqueEvents,ga:eventValue");
service.Management.UnsampledReports.List(results.ProfileInfo.AccountId, results.ProfileInfo.WebPropertyId, results.ProfileInfo.ProfileId).Execute();
service.Management.UnsampledReports.Get(results.ProfileInfo.AccountId, results.ProfileInfo.WebPropertyId, results.ProfileInfo.ProfileId, file_entry.Id).Execute();

我试图通过阅读文档来了解 service.Management.UnsampledReports.Insert 中 GET/POST 的身份验证范围 https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtUnsampledReports 但是当我尝试在我的 C# 程序中插入非抽样报告时,我得到了错误:

The service analytics has thrown an exception: Google.GoogleApiException: Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]

代码是:

UnsampledReport report = new UnsampledReport();
report.Title = "Test the API";
report.StartDate = "2014-10-21";
report.EndDate = "2014-10-22";
report.Metrics = "ga:uniqueEvents,ga:eventValue";
report.Dimensions = "ga:Dimension1, ga:Dimension2, ga:Dimension3, ga:eventAction";
report.Filters = "ga:Dimension1=~10.0.A.;ga:eventAction=~.*100$";
try
{
  service.Management.UnsampledReports.Insert(report, results.ProfileInfo.AccountId, results.ProfileInfo.WebPropertyId, results.ProfileInfo.ProfileId).Execute();
}
catch (Exception google_exception)
{
  Console.WriteLine("{0} UnsampledReports.Insert caught", google_exception);
}

其中“结果”是成功调用的结果 DataResource.GaResource.GetRequest data_request = service.Data.Ga.Get(...GaData results = data_request.Execute();

我能够通过 Google Premium 提供的 Web 界面创建非抽样报告,可以列出这些报告并在 C# 中获取它们,但我无法从 C# 插入非抽样报告。根据文档,应该支持两个指标和四个带有过滤器的维度。我注意到 Web 界面中的平面表只支持二维,但我认为四个应该​​没问题。

我正在使用最新的 Nuget 版本:

using Google.Apis.Auth;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Analytics.v3;
using Google.Apis.Util.Store;
using Google.Apis.Services;
using Google.Apis.Analytics.v3.Data;

我正在使用此身份验证范围:

using (var stream = new FileStream(path_secret + @"\client_secrets.json", FileMode.Open, FileAccess.Read))
{
  credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
    GoogleClientSecrets.Load(stream).Secrets,
    new[] { AnalyticsService.Scope.Analytics, 
            AnalyticsService.Scope.AnalyticsEdit,
            AnalyticsService.Scope.AnalyticsReadonly },
    "Test user",
    CancellationToken.None,
    new FileDataStore("Analytics.Auth.Store")).Result;
}
  1. 有什么方法可以打开 API 代码生成的 URI 的调试打印输出?
  2. Wireshark 是调试此类故障的最佳方法吗?
  3. 我无法通过可用的 .zip 文件找到完整的源代码,我应该在哪里查找?
  4. 谁有关于如何实现调用 service.Management.UnsampledReports.Insert 的提示和技巧?

谢谢!

【问题讨论】:

  • 您登录应用程序的用户。他们有什么级别的访问权限?我怀疑问题出在访问插件上
  • 感谢您的回复。我的错!重新阅读文档后,我意识到我在代码中的第一个范围是 AnalyticsService.Scope.AnalyticsReadonly,并且我接受了支持此范围的 Oauth2 令牌。有点出乎意料的是,在代码中添加两个新范围:Analytics 和 AnalyticsEdit 不会触发对具有正确范围授权的新 OAuth2 令牌的请求。
  • 如果您将“测试用户”更改为其他名称,它将强制他们在您使用 filedatastore 时重新进行身份验证,身份验证以该名称存储在您的电脑上。更改名称将导致它重新验证,因为它找不到它。你只需要第一个它会给你一切。

标签: google-api-dotnet-client


【解决方案1】:

代码中的开发人员范围可能会发生变化,到目前为止,我发现触发新 Oauth2 令牌请求的唯一解决方案是修补凭据请求 GoogleWebAuthorizationBroker.AuthorizeAsync 中的字符串“用户”。这是一个丑陋的解决方法,我希望在代码中更改范围时找到正确的调用来强制请求新的 Oauth2 令牌。更新已部署客户端的替代方法是在“用户”字段中发明某种类型的范围版本管理。这很容易出错并且难以维护。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-01
    • 2021-07-11
    • 2018-11-08
    • 2015-10-15
    • 2016-01-09
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    相关资源
    最近更新 更多