0

尝试针对谷歌分析 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 & "\key\nnnnnnnnnnnnnn-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文件......除此之外,我不知所措

4

1 回答 1

0

我自己回答这个问题的唯一原因是它不会被删除。

我最终要做的就是将服务开发人员的电子邮件地址添加到我的分析帐户中,并且它可以工作!

于 2013-10-03T22:02:57.030 回答