我正在尝试通过服务帐户使用Google AnalyticsAPI获取数据.
我一直在搜索stackoverflow如何做到这一点,并使用几个帖子上找到的完全相同的代码,但遇到问题,让它工作.
资料来源:
服务应用程序和Google Analytics API V3:服务器到服务器OAuth2身份验证?
服务应用程序和Google AnalyticsAPI 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);
Run Code Online (Sandbox Code Playgroud)
执行时我得到以下错误:
致命错误:未捕获的异常"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 /应用/ XAMPP/xamppfiles/htdocs中/见解/应用/库/ googleAPI/IO/Google_REST .PHP(36):Google_REST :: decodeHttpResponse(对象(Google_HttpRequest))#1 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/service/Google_ServiceResource.php(178):Google_REST ::执行(对象(Google_HttpRequest))#2 /Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/contrib/Google_AnalyticsService.php(383):Google_ServiceResource - > __呼叫( 'GET',阵列)#3 /应用/ XAMPP/xamppfiles/htdocs中/见解/应用/库/ Google.php(46):Google_DataGaServiceResource->的get('克/Applications/XAMPP/xamppfiles/htdocs/insights/application/libraries/googleAPI/io/Google_REST.php上线66
因为我使用与工作示例完全相同的代码,我认为它可能是API或服务帐户设置的问题? …