【问题标题】:Permissions error with Google Analytics API and VB.netGoogle Analytics API 和 VB.net 的权限错误
【发布时间】:2017-04-02 17:02:48
【问题描述】:

这是我想要做的事情:我试图在服务器端生成一个访问令牌,用于对用户进行身份验证,如本例所示:

https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/

经过几次尝试,我已经在 VB.net 中编写了这段代码:

Public GA_Token As String

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim filename As String = {{path to p12 file}}
    Dim serviceAccountEmail As String = {{service account generated email from IAM & Admin from console.developers.google.com of the form user@project name.iam.gserviceaccount.com}}
    Dim certificate = New X509Certificate2(filename, "password", X509KeyStorageFlags.Exportable Or X509KeyStorageFlags.MachineKeySet Or X509KeyStorageFlags.PersistKeySet)
    Dim Scopes As IEnumerable(Of String) = {AnalyticsService.Scope.AnalyticsReadonly}
    Dim credential As New ServiceAccountCredential(New ServiceAccountCredential.Initializer(serviceAccountEmail) With {.Scopes = Scopes}.FromCertificate(certificate))
    GA_Token = credential.GetAccessTokenForRequestAsync(Request.Url.ToString, CancellationToken.None).Result

End Sub

然后将 GA_Token 写入我创建的分析页面。

问题是,当我查看报告时,我从 Chrome 收到 403 错误。

Objectbody: "{"error":{"errors":[{"domain":"global","re​​ason":"insufficientPermissions","message":"用户没有任何 Google Analytics 帐户。"} ],"code":403,"message":"用户没有任何 Google Analytics 帐户。"}}"headers: Objectcache-control: "private, max-age=0"content-encoding: "gzip"content-长度:“146”内容类型:“应用程序/json;charset=UTF-8”日期:“格林威治标准时间 2016 年 11 月 18 日星期五 18:25:08”到期:“格林威治标准时间 2016 年 11 月 18 日星期五 18:25:08”服务器:“GSE”变化:“Origin,X-Origin”www-authenticate:“Bearer realm="https://accounts.google.com/", error=insufficient_scope, scope="https://www.googleapis. com/auth/analytics.edit""proto: Object__defineGetter__: defineGetter()defineSetter: defineSetter()lookupGetter: lookupGetter()lookupSetter: lookupSetter()构造函数: Object()hasOwnProperty: hasOwnProperty()isPrototypeOf: isPrototypeOf ()propertyIsEnumerable: 公关opertyIsEnumerable()toLocaleString: toLocaleString()toString: toString()valueOf: valueOf()get proto: proto()set proto: proto()result: Objecterror: Objectcode: 403errors: Array[1]0: Objectlength: 1__proto__: Array[0]message: “用户没有任何 Google Analytics 帐户。”proto:Object__proto__:Objectstatus:403statusText:null__proto__:Object_.nH@cb=gapi.loaded_0:606_.du.Vh@cb=gapi.loaded_0:742(匿名函数)@view-selector2.js:109h.o0@cb= gapi.loaded_0:75xs @ cb=gapi.loaded_0:78Wq @ cb=gapi.loaded_0:78_.C.uea @ cb=gapi.loaded_0:77Ap @ cb=gapi.loaded_0:71 cb=gapi.loaded_0:67 Uncaught Object {result: Object, body: "{"error":{"errors":[{"domain":"global","re​​ason":"i...er 没有任何 Google分析帐户。“}}”,标题:对象,状态:403,状态文本:null}(匿名函数)@ cb=gapi.loaded_0:67

现在,我可以使用 OAuth2 协议生成令牌并在用户登录到他/她的帐户时显示它,但我试图绕过它。我遇到的问题是我看不到应该在哪里设置帐户的权限。我进入 IAM 和 Admin 并在两个不同的服务帐户上启用域范围委派进行测试。我为它们设置了所有可以想象的权限(这适用于所有者 + 18 个其他权限)。我还需要在哪里设置权限,还是我忽略了什么?

【问题讨论】:

  • 哇 16 票赞成,1 票反对……这是我见过的最低票!
  • 很抱歉,我没有看到任何赞成或反对的意见。这是给我的吗?

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


【解决方案1】:

首先,将用户电子邮件设置为this here,不知道如何在VB中做到这一点。

你说:

我可以使用 OAuth2 协议生成令牌并在用户登录到他/她的帐户时显示它,但我试图绕过它。

但我看到您也在尝试使用 Google 服务帐户,我认为只有在您是付费 G Suite 用户时才有效。

我花了很多时间为我的Gmail client 解决这个问题,并偶然找到了解决方案: 在Connected Apps 下,每个用户都应该可以看到权限。 但这确实意味着您的用户必须手动授予访问权限一次,之后您就可以使用 Google 服务帐户获取令牌。

【讨论】:

  • FWIW,我不是付费 G Suite 用户。
  • 我把赏金给了 Kape,因为他在另一个帖子中发布的用户属性答案解决了我的问题。
  • 我已经更新了我的答案,我遇到了这两个问题,但可能是因为您使用的是其他 Google 服务,所以第二部分在您的情况下不是问题。
【解决方案2】:

我试过了,它对我有用。

我之前设置了一个名为“YYYassistant”的项目并创建了一个帐户:

然后我创建了一个简单的 WinForm 应用程序,但无法重现该问题,这可行:

Imports Google.Apis.Auth.OAuth2
Imports System.Security.Cryptography.X509Certificates

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim scopes As String() = New String() {AnalyticsService.Scope.Analytics}
        ' view and manage your Google Analytics data
        Dim keyFilePath = "C:\Temp\YYYassistant-1234db98765.p12"
        ' Downloaded from https://console.developers.google.com
        Dim serviceAccountEmail = "XXX@YYYassistant.iam.gserviceaccount.com"
        ' found https://console.developers.google.com
        'loading the Key file
        Dim certificate = New X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable)
        Dim credential = New ServiceAccountCredential(New ServiceAccountCredential.Initializer(serviceAccountEmail) With {.Scopes = scopes}.FromCertificate(certificate))
    End Sub
End Class

"code":403,"message":"用户没有任何 Google Analytics 帐户。"

您的帐户有问题,请创建一个新帐户。

【讨论】:

    【解决方案3】:

    这个问题在 Json 上有解释

    "{"error":{"errors":[{"domain":"global","reason":"insufficientPermissions","message":"User does not have any Google Analytics account."}]
    
    { "User does not have any Google Analytics account." } 
    

    也许更改代码不会改变任何东西,因为这是访问错误。 总而言之,用于获取 Token 的 You gmail 没有关联账户。 Auth 的验证过程看起来很好。在更改代码之前,我建议您执行以下步骤。

    1.- 获取有效的电子邮件和密码

    我可以相信你已经得到了这个(你已经有了令牌),抓住这个凭据并转到

    https://analytics.google.com/analytics/web/ ,使用这些凭据登录并查看您是否已关联任何帐户。

    如果您的帐户无权访问,您将看到

    注意:如果您使用 Oauth 验证,用于获取令牌的电子邮件可能与项目中使用的电子邮件不同,您可能需要使用其他人的凭据。 100% 确定您使用的是什么凭证。

    2.- 如果您没有帐户,则需要请求访问权限,因此拥有 Google Analytics(分析)帐户的用户需要授予您访问权限。在所需的帐户中,您需要进入 Admin -> User Management ,并添加验证过程中使用的电子邮件: 拥有令牌后,请记住将其存储并在需要时刷新,令牌需要进行管理

    3.- 如果您想获取自己的数据,也许您可​​以使用 P12key,正如 Jeremy 所说,这种方法更容易验证,但是您需要在每个查询上添加 p12key(通常库会这样做为你)。但是使用这种方法,您只能访问与您自己的 gmail 帐户相关联的数据。如果您更改方法,您可能会遇到相同的错误,即它需要 100% 的已使用帐户

    【讨论】:

      猜你喜欢
      • 2021-07-11
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 2014-12-04
      • 2016-11-25
      相关资源
      最近更新 更多