I'm trying to fetch data with the Google Analytics API through service accounts.
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.
Sources:
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
This is what i got:
require_once('googleAPIGoogle_Client.php'); require_once('googleAPI/contrib/Google_AnalyticsService.php'); const CLIENT_ID = 'xxxxxxxx001.apps.googleusercontent.com'; const SERVICE_ACCOUNT_NAME = '[email protected]'; $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);
when executing i got the following error:
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
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 :
and this is api access accounts:
Does anybody know what i'm doing wrong?
To get started, you'll need to sign into your Google Analytics account. Then, click the Admin option at the bottom of your Analytics panel on the left. Now, you'll see the 3 levels of access (i.e. Account, Property, and View). And, you'll also find the User Management option below all 3 access levels.
I was getting exactly the same error and this solved it for me:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With