Google API v3 .Net客户端请求中未经授权的客户端或范围

时间:2015-04-27 17:17:48

标签: google-analytics-api google-oauth2

我收到错误消息“请求中未经授权的客户端或范围”。执行以下代码时。如果我删除.User = myUserEmail参数,则会收到“权限不足[403]”错误消息。如果我不得不猜测,我猜这个问题是我正在使用的.user参数。但是,我不知道那里我做错了什么。我使用的是与通常用于登录Google Analytics的Google帐户相关联的电子邮件地址。

    Private myClientEmail As String = System.Configuration.ConfigurationManager.AppSettings("clientEmail").ToString()
    Private myP12Path As String = System.Configuration.ConfigurationManager.AppSettings("p12Path").ToString()
    Private myP12Password As String = System.Configuration.ConfigurationManager.AppSettings("p12Password").ToString()
    Private myUserEmail As String = System.Configuration.ConfigurationManager.AppSettings("userEmail").ToString()
    Private myApplicationName As String = System.Configuration.ConfigurationManager.AppSettings("applicationName").ToString()

    Dim scopes As IList(Of String) = New List(Of String)()
    scopes.Add("https://www.googleapis.com/auth/drive.readonly")

    Dim certificate As X509Certificate2 = New X509Certificate2(myP12Path, myP12Password, X509KeyStorageFlags.Exportable)

    Dim credential As ServiceAccountCredential = New ServiceAccountCredential(New ServiceAccountCredential.Initializer(myClientEmail) With {
                                                                                .Scopes = scopes,
                                                                                .User = myUserEmail
                                                                              }.FromCertificate(certificate))

    Dim service As AnalyticsService = New AnalyticsService(New BaseClientService.Initializer() With {
                                                                                    .HttpClientInitializer = credential,
                                                                                    .ApplicationName = myApplicationName
                                                                                })

    Dim profileId As String = "ga:12345678"
    Dim startDate As String = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd")
    Dim endDate As String = DateTime.Today.ToString("yyyy-MM-dd")
    Dim metrics As String = "ga:visits"
    Dim request As DataResource.GaResource.GetRequest = service.Data.Ga.Get(profileId, startDate, endDate, metrics)
    request.Dimensions = "ga:date"
    Dim data As GaData = request.Execute()

1 个答案:

答案 0 :(得分:0)

我发现了错误......

在某些地方,我无意中将范围更改为" drive.readonly"。当我尝试使用" analytics.readonly"时。