您好,我尝试使用服务帐户在 ASP.NET/C# 中使用 Google Analytics API Ver 3,但似乎无法正常工作。一切都编译,但调用 GaData d = r.Execute(); 我收到一个 GoogleApiException ““ids”的参数验证失败。任何可能导致此问题的信息/帮助将不胜感激。
//This is the API url which we're storing to a string
string scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue();
//For whatever reason, this is labelled wrong. It is the email address
//that you have added as a user to your Analytics account
string clientId = "***********@developer.gserviceaccount.com";
//This is the physical path to the key file file.
string keyFile = @"C:\key\***********.p12";
//The password Google gives you.
string keyPassword = "notasecret";
//Store the authentication description
AuthorizationServerDescription desc = GoogleAuthenticationServer.Description;
//Create a certificate object to use when authenticating
X509Certificate2 key = new X509Certificate2(keyFile, keyPassword, X509KeyStorageFlags.Exportable);
//Now, we will log in and authenticate, passing in the description
//and key from above, then setting the accountId and scope
AssertionFlowClient client = new AssertionFlowClient(desc, key)
{
ServiceAccountId = clientId,
Scope = scope
};
//Finally, complete the authentication process
//NOTE: This is the first change from the update above
OAuth2Authenticator<AssertionFlowClient> auth =
new OAuth2Authenticator<AssertionFlowClient>(client, AssertionFlowClient.GetState);
//First, create a new service object
AnalyticsService gas = new AnalyticsService(new BaseClientService.Initializer() { Authenticator = auth });
//Create our query
//The Data.Ga.Get needs the parameters:
//Analytics account id, starting with ga:
//Start date in format YYYY-MM-DD
//End date in format YYYY-MM-DD
//A string specifying the metrics
DataResource.GaResource.GetRequest r =
gas.Data.Ga.Get("ga:nnnnnnnn", "2013-01-01", "2013-01-31", "ga:visitors");
//Specify some addition query parameters
r.Dimensions = "ga:pagePath";
r.Sort = "-ga:visitors";
r.MaxResults = 5;
//Execute and fetch the results of our query
GaData d = r.Execute();
1.全栈跟踪
在 Google.Apis.Http.HttpClientFactory.CreateHandler(CreateHttpClientArgs args) 在 Google.Apis.Http.HttpClientFactory.CreateHttpClient(CreateHttpClientArgs args) 在 c:\code.google.com\google-api-dotnet-client\default\Tools\ BuildRelease\bin\Release\release140\default\Src\GoogleApis\Apis\Http\HttpClientFactory.cs:第 36 行 Google.Apis.Services.BaseClientService.CreateHttpClient(Initializer initializer) in c:\code.google.com\google- api-dotnet-client\default\Tools\BuildRelease\bin\Release\release140\default\Src\GoogleApis\Apis\Services\BaseClientService.cs:Google.Apis.Services.BaseClientService..ctor(Initializer initializer) 中的第 220 行c:\code.google.com\google-api-dotnet-client\default\Tools\BuildRelease\bin\Release\release140\default\Src\GoogleApis\Apis\Services\BaseClientService.cs:Google.Apis 的第 182 行。分析.v3。AnalyticsService..ctor(Initializer initializer) at GoogleAnalyticDotNet.Program.Main(String[] args) in d:\GoogleAnalyticDotNet\GoogleAnalyticDotNet\GoogleAnalyticDotNet\Program.cs: System.AppDomain._nExecuteAssembly 的第 58 行(RuntimeAssembly 程序集,String[] args ) 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext 的 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 的 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)。 RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 在 System.Threading.ExecutionContext。在 System.Threading.ThreadHelper.ThreadStart() 处运行(ExecutionContext executionContext,ContextCallback 回调,对象状态)