我正在尝试从 .NET 应用程序连接到 Google Analytics API。这是我想出的代码:
Public Function GetData() As String
Dim client As AssertionFlowClient = New AssertionFlowClient(GoogleAuthenticationServer.Description,
New X509Certificate2("C:\Users\xxxxx\privatekey.p12", "notasecret",
X509KeyStorageFlags.Exportable Or X509KeyStorageFlags.MachineKeySet)) With {
.Scope = "analytics.readonly",
.ServiceAccountId = "xxxxx@developer.gserviceaccount.com"}
Dim authenticator As OAuth2Authenticator(Of AssertionFlowClient) = New OAuth2Authenticator(Of AssertionFlowClient)(client, AddressOf AssertionFlowClient.GetState)
Dim service As AnalyticsService = New AnalyticsService(authenticator)
Dim profileId As String = "ga:xxxxx"
Dim startDate As String = "2013-02-15"
Dim endDate As String = "2013-02-20"
Dim metrics As String = "ga:visitors"
Dim request As DataResource.GaResource.GetRequest = service.Data.Ga.Get(profileId, startDate, endDate, metrics)
Dim data As GaData = request.Fetch()
Return data.ToString()
End Function
request.Fetch()
我的问题是,当我点击此方法时,我在“发送直接消息或获取响应时发生错误”行出现异常。内部异常说:“远程服务器返回错误:(400)错误请求。”
这个错误显然很模糊。根据Google 的错误代码文档, 400 Bad Request 表明输入参数在某种程度上没有意义。任何人都可以通过查看我的代码中的参数来诊断问题吗?否则,我怎么会开始追查我做错了什么?错误中几乎没有任何信息。
(至少我已经通过了身份验证阶段......我认为。)