Google Analytics API Returning 401 On Example Code

2019-02-27 15:22发布

I am attempting to retrieve the data from our content experiments from google analytics...

I am using the following code, my creds are good, and have been censored for this post...

<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

session_start();

$client = new Google_Client();
$client->setApplicationName('Hello Analytics API Sample');

// Visit https://cloud.google.com/console to generate your
// client id, client secret, and to register your redirect uri.
$client->setDeveloperKey('xxxxx');

$service = new Google_Service_Analytics($client);
try {
$results = $service->management_experiments->listManagementExperiments('xxxx', 'xxxx', 'xxxx');
} catch (apiServiceException $e) {
  print 'There was an Analytics API service error ' . $e->getCode() . ':' . $e->getMessage();

} catch (apiException $e) {
  print 'There was a general API error ' . $e->getCode() . ':' . $e->getMessage();
}
echo '<pre>';
print_r($results);
echo '</pre>';

I am using the following example ....

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtExperimentsGuide#list

Any ideas on why I am getting a 401 unauthorized? That a login is required?

1条回答
Deceive 欺骗
2楼-- · 2019-02-27 16:05

The problem is that you haven't Authorized access to your data yet. Since you say its only your own data you want to access i sugest you look into a service account. By setting up a service account in Google apis console it will allow you to access your own data with out needing to login and autenticate the code all the time.

Check the following link. Read though Before you begin make sure you do all that.

https://developers.google.com/analytics/devguides/config/mgmt/v3/mgmtExperimentsGuide#service

  1. Register your application in the Google Developers Console
  2. Authorize access to Google Analytics data.
  3. Create an Analytics service object

You have skiped the first two steps and gone directly to step 3 creating the service object. Once you have done step 1 you can use the following code for step 2.


Here is an example of how to use a service account in php ServiceAccount

That sample project is for the PredictionService not the google analytics service. You need to edit it slightly.

require_once '../../src/Google/Client.php'; 
require_once '../../src/Google/Service/Analytics.php'; 

// Set your client id, service account name, and the path to your private key. 
// For more information about obtaining these keys, visit: 
// https://developers.google.com/console/help/#service_accounts

const CLIENT_ID = 'INSERT_YOUR_CLIENT_ID'; 
const SERVICE_ACCOUNT_NAME = 'INSERT_YOUR_SERVICE_ACCOUNT_NAME'; 


// Make sure you keep your key.p12 file in a secure location, and isn't 
// readable by others.

const KEY_FILE = '/super/secret/path/to/key.p12'; 


$client = new Google_Client(); 
$client->setApplicationName("Google Analytics Sample"); 


// Load the key in PKCS 12 format (you need to download this from the 
// Google API Console when the service account was created. 


$client->setAssertionCredentials(new Google_AssertionCredentials( 
    SERVICE_ACCOUNT_NAME(Email), 
    array('https://www.googleapis.com/auth/analytics.readonly'), 
    file_get_contents(KEY_FILE)) 
); 


$client->setClientId(CLIENT_ID); 
$service = new Google_Service_Analytics($client);

Now you have $service that you can use with the rest of your calls. Note: I didnt have time to test that code let me know if it doesnt work and i will give you a hand in fixing it.

查看更多
登录 后发表回答