我正在尝试连接到 Google Analytics 以检索一些信息并将其放入数据库中,到目前为止,我的代码是这样的:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
// Google API
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Services;
using Google.Apis.Util;
namespace CCQ.GoogleToSharePoint
{
class Program
{
static void Main(string[] args)
{
//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 = "<redacted>@gmail.com";
//This is the physical path to the file we downloaded earlier
//To demonstrate this, I've kept the full path to my key file.
//Obviously, you will need to change this to match where you've
//stored yours.
string keyFile = @"<redacted>";
//The password Google gives you, probably the same as the one below
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
//NOTE: this is the second change from the update
//above. Thanks to James for pointing this out
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:<redacted>", "2013-09-09", "2013-09-23", "ga:visitors");
//Specify some addition query parameters
r.Dimensions = "ga:visitorType";
r.Sort = "-ga:visitors";
r.MaxResults = 5;
//Execute and fetch the results of our query
try
{
//Write the column headers
GaData d = r.Execute();
foreach (var h in d.ColumnHeaders)
{
Console.WriteLine(h.Name);
}
//Write the data
foreach (var row in d.Rows)
{
Console.WriteLine(row[0] + " ------ " + row[1]);
}
}
catch (ProtocolException webEx)
{
Console.WriteLine("Protocol Exception: {0}\n\n", webEx.ToString());
Console.WriteLine("Stack Trace: {0}", webEx.StackTrace);
throw;
}
}
}
}
不幸的是,我在我的执行行上抛出了一个错误,特别是这个:
//Write the column headers
GaData d = r.Execute();
错误本身是:
发送直接消息或获取响应时出错。
Google 的 API 文档说 400:Bad Response 标头将在以下条件下发送:
400 Bad Request 错误请求的类型包括:
无效的维度和/或指标
数量:要么没有指标,要么维度/指标过多
在一侧是指标而另一侧是维度的过滤器上使用 OR
无效的过滤器/段语法
非法维度/度量组合或高级段
但是,据我所见,我没有遇到任何这些-所以我永远感到困惑。我该如何进一步调试此问题?
此错误的堆栈跟踪是:
StackTrace" 在 c:\code.google.com\google-api-dotnet-client\default_3\Tools\Google.Apis.Release\bin\Debug\output\ 中的 Google.Apis.Requests.ClientServiceRequest`1.Execute()默认\Src\GoogleApis\Apis\Requests\ClientServiceRequest.cs:line 96\r\n 在 i:\dev\CCQ.GoogleToSharePoint\CCQ.GoogleToSharePoint\Program 中的 CCQ.GoogleToSharePoint.Program.Main(String[] args)。 cs:line 121\r\n at System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,String[] args)\r\n at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)\r\n at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()\r\n 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)\r\n 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback,对象状态,布尔值 preserveSyncCtx)\r\n 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调,对象状态,布尔值 preserveSyncCtx)\r\n 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback 回调,对象状态)\r\n 在 System.Threading.ThreadHelper.ThreadStart()" 字符串