我的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);
}
其中'results'是成功调用的结果
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;
}
谢谢!
答案 0 :(得分:0)
开发人员范围可能会在代码中发生变化,到目前为止,我发现触发新Oauth2令牌请求的唯一解决方案是修补字符串' user'在凭证请求中GoogleWebAuthorizationBroker.AuthorizeAsync。这是一个丑陋的解决方法,我希望找到正确的调用,以便在代码中更改范围时强制请求新的Oauth2令牌。更新已部署客户端的替代方案是在“用户”中发明某种类型的范围版本管理。领域。这很容易出错并且难以维护。