1

我正在嘗試使用Google.Apis.Analytics.v3(1.7.0.81-beta)獲取一些基本的分析數據。 我一直在尋找很多的例子和幫助,但大多數似乎已被棄用。谷歌Apis .Net - 獲取分析數據

這是我得到了現在:

var serviceAccountEmail = "[email protected]"; 

var certificate = new X509Certificate2(@"PATH-TO-MY-PRIVATE-KEY", "notasecret", X509KeyStorageFlags.Exportable); 

var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail) 
{ 
    Scopes = new[] { AnalyticsService.Scope.Analytics } 
}.FromCertificate(certificate)); 

// Create the service. 
var gas = new AnalyticsService(new BaseClientService.Initializer() 
{ 
    HttpClientInitializer = credential, 
    ApplicationName = "Tester", 
}); 

var r = gas.Data.Ga.Get("ga:XXXXX", "2013-01-01", "2014-02-31", "ga:visitors"); 

//Specify some addition query parameters 
r.Dimensions = "ga:pagePath"; 
r.Sort = "-ga:visitors"; 
r.MaxResults = 5; 

//Execute and fetch the results of our query 
GaData d = r.Execute(); 

這給了我:

Google.Apis.Requests.RequestError 
Access Not Configured. Please use Google Developers Console to activate the API for your project. [403] 
Errors [ 
    Message[Access Not Configured. Please use Google Developers Console to activate the API for your project.] Location[ - ] Reason[accessNotConfigured] Domain[usageLimits] 
] 

在谷歌API控制檯我已經已經設置一個的OAuth2服務帳戶,並從該添加的電子郵件,我的分析帳戶在網頁上試圖從中提取數據。 所以不確定什麼是錯過?或者如果我實際上只是做錯了?

更新

使分析API後,我現在得到:

Google.Apis.Requests.RequestError User does not have sufficient permissions for this profile. [403] 
    Errors [ 
     Message[User does not have sufficient permissions for this profile.] 
     Location[ - ] 
     Reason[insufficientPermissions] 
     Domain[global] 
    ] 

回答

0

是否啓用Analytics(分析)API一樣顯示在下面的圖片?

enter image description here

+0

我實際上忘了(facepalm),但現在即時通訊:'Google.Apis.Requests.RequestError 用戶對此配置文件沒有足夠的權限。 [403] 錯誤[ ]消息[用戶對此配置文件沒有足夠的權限。]位置[ - ]原因[insufficientPermissions]域[global] ]' –

1

添加服務帳戶郵件到谷歌Analytics帳戶您試圖以「閱讀和分析」權限來訪問。

請參閱用於服務帳戶在這裏的答案完全設置好訪問的步驟:

Connecting to Google Analytics API in a Rails app

我知道它說:「導軌」,只看設立服務帳戶的步驟。

+0

我已經這樣做了,所以我仍然認爲它可能是我的碼? –