“权限不足" google Analytics API服务帐户 [英] "Not sufficient permissions" google analytics API service account
问题描述
我正在尝试通过Google Analytics(分析)API通过服务帐户获取数据.
I'm trying to fetch data with the Google Analytics API through service accounts.
我一直在研究stackoverflow如何做到这一点,并一直使用在几篇文章中找到的完全相同的代码,但是在使它工作时遇到了问题.
I have been searching on stackoverflow how to do this and been using the exact same code found on several posts but getting problems to get it working.
来源:
服务应用程序和Google Analytics API V3:服务器到服务器的OAuth2身份验证?
服务应用程序和Google Analytics API V3 :错误101(net :: ERR_CONNECTION_RESET)
这就是我得到的:
require_once('googleAPIGoogle_Client.php');
require_once('googleAPI/contrib/Google_AnalyticsService.php');
const CLIENT_ID = 'xxxxxxxx001.apps.googleusercontent.com';
const SERVICE_ACCOUNT_NAME = 'xxxxxx001@developer.gserviceaccount.com';
$keyfile = $_SERVER['DOCUMENT_ROOT']."/xxxxxxx284-privatekey.p12";
$client = new Google_Client();
$client->setAccessType('offline');
$client->setApplicationName("cc insights");
$key = file_get_contents($keyfile);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/analytics.readonly'),
$key)
);
$service = new Google_AnalyticsService($client);
$data = $service->data_ga->get("ga:xxxx7777", "2012-01-01", "2013-01-25", "ga:pageviews");
var_dump($data);
执行时出现以下错误:
Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling GET https://www.googleapis.com/analytics/v3/data/ga?ids=ga%3Axxxx7777&start-date=2012-01-01&end-date=2013-01-25&metrics=ga%3Apageviews: (403) User does not have sufficient permissions for this profile.' in /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php:66 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/service/Google_ServiceResource.php(178): Google_REST::execute(Object(Google_HttpRequest)) #2 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/contrib/Google_AnalyticsService.php(383): Google_ServiceResource->__call('get', Array) #3 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/Google.php(46): Google_DataGaServiceResource->get('g in /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php on line 66
因为我使用的代码与工作示例完全相同,所以我认为API或服务帐户设置可能有问题吗?
Because i'm using the exact same code as working examples, i think it might be a problem with the API or service account settings?
这是我添加到分析帐户的用户:
This is the user that i added to the analytics account :
,这是api访问帐户:
and this is api access accounts:
有人知道我在做什么错吗?
Does anybody know what i'm doing wrong?
推荐答案
我遇到了完全相同的错误,这为我解决了这个问题:
I was getting exactly the same error and this solved it for me:
我使用的是帐户ID而不是查看ID.切换到View ID即可解决问题-它始终是"View ID",而不是帐户或媒体资源ID(看起来像"UA-xxx").查看ID用于特定的网络媒体资源,可以在管理"->查看"(第三列)->查看设置"中找到.这是一个不带破折号的整数.
I was using the account ID instead of the View ID. Switching to the View ID fixed it - it’s always the "View ID" and not the account or property ID (which looks like "UA-xxx"). The View ID is for a specific web property and can be found in the Admin->View (3rd column)->View Settings. It’s an integer with no dashes.
https://stackoverflow.com/a/15789266/1391229
这篇关于“权限不足" google Analytics API服务帐户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!