Google AnalyticsAPI v3 OAuth2Authenticator问题

时间:2013-10-03 20:09:59

标签: .net vb.net google-analytics google-api google-analytics-api

在尝试使用googles analytics 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是正确的,{} {}在我的API控制台向我展示时是正确的。

我能想到的另一件事就是重新尝试生成.p12文件...除此之外,我不知所措

1 个答案:

答案 0 :(得分:0)

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

我最终要做的是将服务开发人员的电子邮件地址添加到我的分析帐户,它确实有效!