2016-11-18 62 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服務,第二部分對您而言不是問題。 –