我在尝试对谷歌的分析API进行身份验证时遇到了一个奇怪的问题。
有人可以帮忙吗?
代码如下:
Imports DotNetOpenAuth.OAuth
Imports Google.Apis.Analytics.v3
Imports Google.Apis.Analytics.v3.Data
Imports Google.Apis.Authentication.OAuth2
Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
Imports Google.Apis.Services
Imports Google.Apis.Util
Imports DotNetOpenAuth.OAuth2
Imports System.Security.Cryptography.X509Certificates
Public Class GoogleAnalytics
Private _scope = AnalyticsService.Scopes.AnalyticsReadonly.GetStringValue()
Private _clientID As String = "nnnnnnnnnnnnnn@developer.gserviceaccount.com" '"1038052825878.apps.googleusercontent.com"
Private _keyFile As String = AppDomain.CurrentDomain.BaseDirectory & "keynnnnnnnnnnnnnn-privatekey.p12"
Private _keyPassword As String = "notasecret"
Private Function Auth() As OAuth2Authenticator(Of AssertionFlowClient)
Dim _desc As New AuthorizationServerDescription
_desc = GoogleAuthenticationServer.Description
Dim _key As X509Certificate2 = New X509Certificate2(_keyFile, _keyPassword, X509KeyStorageFlags.Exportable)
Dim _client As AssertionFlowClient = New AssertionFlowClient(_desc, _key) With {.ServiceAccountId = _clientID, .Scope = _scope}
Return New OAuth2Authenticator(Of AssertionFlowClient)(_client, AddressOf AssertionFlowClient.GetState)
End Function
Public Sub Analytics()
Dim _gas As AnalyticsService = New AnalyticsService(New BaseClientService.Initializer() With {.Authenticator = Auth()})
Dim _r As DataResource.GaResource.GetRequest = _gas.Data.Ga.Get("ga:nnnnnnnn",
"2013-01-01",
"2013-01-31",
"ga:visitors")
_r.Dimensions = "ga:pagePath"
_r.Sort = "-ga:visitors"
_r.MaxResults = 5
Dim _d As GaData = _r.Execute() '<------ ERROR HAPPENS HERE
For Each h In _d.ColumnHeaders
Console.WriteLine(h.Name)
Next
For Each row In _d.Rows
Console.WriteLine(row(0) & " ------ " + row(1))
Next
Console.ReadKey()
End Sub
End Class
我得到的错误是:
Google.Apis.Requests.RequestError
User does not have any Google Analytics account. [403]
Errors [
Message[User does not have any Google Analytics account.] Location[ - ] Reason[insufficientPermissions] Domain[global]
]
ga: ID 是正确的,并且_clientID
在我的 API 控制台显示的情况下是正确的。
我唯一能想到的另一件事是重新尝试生成 .p12 文件......除此之外,我不知所措
我自己回答这个问题的唯一原因是这样就不会被删除。
我最终不得不做的是将服务开发人员电子邮件地址添加到我的分析帐户中,它有效!