2016-11-18 61 views
0

这是我想要做的:我试图在服务器端生成一个访问令牌,将用于验证用户,如在此示例中:权限错误谷歌分析API和VB.net

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 [email protected] 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错误。 “{”error“:{”errors“:[{”domain“:”global“,”reason“:”insufficientPermissions“,”message“:”用户没有任何Google Analytics帐户。“)。 }],“code”:403,“message”:“用户没有任何Google Analytics帐户。”}}“headers:Objectcache-control:”private,max-age = 0“content-encoding:”gzip“content -length:“146”content-type:“application/json; charset = UTF-8”日期:“2016年11月18日星期五18:25:08 GMT”过期时间:“2016年11月18日星期五18:25:08 GMT “服务器:”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()defineSetterdefineSetter()lookupGetterlookupGetter()lookupSetterlookupSetter()构造:对象()hasOwnProperty:hasOwnProperty()isPrototypeOf:isPrototypeOf()propertyIsEnumerable:propertyIsEnumerable()的toLocaleString:的toLocaleString()的toString:的toString()的valueOf:的valueOf()得到()设定()结果:Objecterror:目标代码:403errors:数组[1] 0:Objectlength:1__proto__:数组[0]消息:“用户没有任何Google Analytics帐户。” :Object__proto__:Objectstatus:403statusText:null__proto__:Object_.nH @ CB = gapi.loaded_0:606_.du.Vh @ CB = gapi.loaded_0:742(匿名功能)@视图-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未捕获的对象{result:Object,body:“{”error“:{”errors“:[{”domain“:”global“,”reason“:”我...呃没有任何Google Analytics帐户。 “,headers:Object,status:403,statusText:null}(匿名函数)@ cb = gapi.loaded_0:67

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

+0

Wow 16 upvotes,1 downvote ...这是我见过的最低! –

+0

对不起,但我没有看到任何upvotes或downvotes。这是给我的吗? – SEFL

回答

0

我试过了,它适用于我。

我以前有安装一个名为“YYYassistant”的项目,我创建了一个帐户:

enter image description here

然后,我创建了一个简单的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 = "[email protected]" 
     ' 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":"User does not have any Google Analytics account."

它的问题与您的帐户,做一个新的。

0

的问题上的Json

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

{ "User does not have any Google Analytics account." } 

解释也许改变代码不会改变任何东西,因为它是一个访问错误。 简而言之,您用于获取令牌的Gmail已没有帐户链接。 Auth查看井的验证过程。更改代码之前,我建议您执行以下步骤。

1.-获得一个有效的电子邮件和密码

我可以相信你已经有了这个(你已经有了托克),抓住这个凭据,去

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

如果您的帐户中有没有访问,您将看到 enter image description here

注:如果您使用OAuth验证,用于获取电子邮件令牌可以是不同的,关于项目中使用的一个,也许你需要使用他人的凭据。 100%确信你使用的是什么凭证。

2:如果您有需要请访问没有户口,所以与谷歌Analytics帐户,用户需要给你的访问。在需要的帐户中,您需要进入管理 - >用户管理,并添加验证过程中使用的电子邮件: enter image description here 当您拥有令牌时,请记住存储并在需要时刷新它,令牌需要被管理

3.-如果你想获得你自己的数据,也许你可以使用一个p12key,杰里米说,这种方法比较容易验证,但你需要添加在每次查询p12key(通常是库做它为你)。但使用这种方法,您只能访问链接到您自己的Gmail帐户的数据。如果你改变方法你可以有同样的错误,它需要100%的使用帐户

1

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

你说:

I can use the OAuth2 protocol to generate the token and have it display if the user logs in to his/her account, but I'm trying to bypass that.

但我看到,你也尝试使用谷歌的服务帐户,我认为只有当你是付费摹Suite用户的工作。

我花了很多时间来弄清楚这一点对我Gmail client和意外找到了解决办法: 的权限应Connected Apps下每用户可见。 但是,这确实意味着您的用户必须手动授予访问权限,之后您才可以使用Google服务帐户获取令牌。

+0

FWIW,我不是付费的G套件用户。 –

+0

我给了Kape赏金,因为这是他在另一个解决我的问题的线程中发布的用户属性答案。 – SEFL

+0

我已更新我的答案,我遇到了这两个问题,但可能是因为您使用的是其他Google服务,第二部分对您而言不是问题。 –