【发布时间】:2013-01-09 15:19:49
【问题描述】:
我正在尝试通过服务帐户使用 Google Analytics API 获取数据。
我一直在 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)
这是我得到的:
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 在第 66 行
因为我使用的代码与工作示例完全相同,我认为这可能是 API 或服务帐户设置的问题?
这是我添加到分析帐户的用户:
这是 api 访问帐户:
有人知道我做错了什么吗?
【问题讨论】:
-
刚刚开始使用 API 并遇到了类似的问题 - 如果您解决了问题,请告诉我?
-
现在面临同样的问题。
标签: php google-analytics-api jwt