“权限不足”谷歌分析API服务帐户

时间:2013-01-25 16:04:23

标签: php google-analytics-api jwt

我正在尝试通过服务帐户使用Google AnalyticsAPI获取数据。

我一直在stackoverflow上搜索如何做到这一点,并使用了几个帖子上找到的完全相同的代码,但遇到问题让它工作。

来源:

Service Applications and Google Analytics API V3: Server-to-server OAuth2 authentication?

Service Applications and Google Analytics API V3: Error 101 (net::ERR_CONNECTION_RESET)

http://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php

这就是我得到的:

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);

执行时我收到以下错误:

  

致命错误:未捕获的异常'Google_ServiceException',显示消息'错误调用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)用户对此配置文件没有足够的权限。在/Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php:66堆栈跟踪:#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第66行

因为我使用与工作示例完全相同的代码,我认为它可能是API或服务帐户设置的问题?

这是我添加到分析帐户的用户: Analytics users

这是api访问帐户: api access

有人知道我做错了吗?

11 个答案:

答案 0 :(得分:70)

我得到完全相同的错误,这解决了我:

我使用的是帐户ID而不是View ID。切换到视图ID修复它 - 它始终是“视图ID”而不是帐户或属性ID(看起来像“UA-xxx”)。视图ID适用于特定的网络媒体资源,可在管理员 - >视图(第3列) - >视图设置中找到。这是一个没有破折号的整数。

https://stackoverflow.com/a/15789266/1391229

答案 1 :(得分:10)

今天遇到同样的问题,很难找到profileId,你可以在分析中的每个网址中找到它{1} 这是ZZZZZZZ部分

答案 2 :(得分:5)

同样的问题。通过使用VIEW ID而不是帐户ID(UA-XXXXX-1)来解决它。

Analytics Console > Admin -> View (Profile) -> View Settings -> View ID

确保将服务帐户添加到Google Analytics控制台中的用户列表中。只需在开发人员控制台的凭据,API和权限部分进行设置,仍然无法授予其访问分析的权限。

Analytics Console > Admin -> Account -> User Management -> "Add permissions for:"

答案 3 :(得分:4)

得到了同样的错误,但过了一段时间我才认识到我正在使用这个脚本和我的谷歌浏览器(我自己的电子邮件地址),但Analytics API正在公司电子邮件上运行。因此,解决方案可能是编辑Google Chrome的设置。只需断开谷歌浏览器(设置)中的Gmail帐户,然后重新测试即可。

答案 4 :(得分:4)

在“管理”标签上使用“查看ID非帐户ID”,“查看设置”,“查看ID”。

答案 5 :(得分:3)

在试图寻找解决方案2周后帮助了我很多:

  

Analytics控制台 - >管理员 - >查看(个人资料) - >查看设置 - >查看ID

答案 6 :(得分:1)

在另一个应用程序上,我收到“权限不足”,发现我的.htaccess文件有一些文件限制导致了这个问题。

答案 7 :(得分:1)

我在使用.net库时遇到了同样的问题,经过一些修补,我找到了解决方案:

转到您的谷歌分析管理网站&将服务帐户添加为用户&授予该帐户必要的权利

答案 8 :(得分:0)

可能是您正在使用其他客户端ID:客户端密码:获取其他个人资料ID详细信息

答案 9 :(得分:0)

遇到问题..结果是帐户ID隐藏在您的分析帐户的网址中!

https://developers.google.com/analytics/devguides/reporting/core/v3/#user_reports

网址通常如下:

https://www.google.com/analytics/web/?hl=en#management/Settings/a51343283w84330433 p87396224 /%3Fm.page%3DAccountSettings /

我们正在寻找的ID是字母p。

在上面的示例中,将起作用的ID是:87396224

答案 10 :(得分:0)