How to run Google Embed API Server-side Authorizat

2019-04-15 01:18发布

I am trying to run the Google Embed API Server-side Authorization demo, here: https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/, but using PHP instead of Python for Step 3 (Using the JSON key data to request an access token).

I have installed the Google API PHP client: https://github.com/google/google-api-php-client.

I have created a Service Account (https://console.developers.google.com), enabled Analytics API for it, downloaded the JSON key file, and copied that to my web server (below web root level for security).

I have registered the Service Account in the Google Analytics account (read & analyse only).

I am trying to run the demo using replacement PHP code from here https://stackoverflow.com/a/32767845/1803239:

<?php    

// Load the Google API PHP Client Library.
require_once 'google-api-php-client/src/Google/autoload.php';

// Start a session to persist credentials.
session_start();

// Create the client object and set the authorization configuration
// from the client_secretes.json you downloaded from the developer console.
$client = new Google_Client();
$client->setAuthConfigFile('/path/to/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);


// If the user has already authorized this app then get an access token
// else redirect to ask the user to authorize access to Google Analytics.
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  // Set the access token on the client.
   $client->setAccessToken($_SESSION['access_token']);

  // Create an authorized analytics service object.
  $analytics = new Google_Service_Analytics($client);


  // Get the results from the Core Reporting API and print the results.

} else {
  $redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/oauth2callback.php';
  header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}


//get the access token
$myToken = json_decode($client->getAccessToken());
?>

Running the demo (in a file named test3.php, with the PHP code instead of the Python code) fails at this line, $client->setAuthConfigFile('client_secrets.json');, with the following error:

Fatal error: Uncaught exception 'Google_Exception' with message 'Invalid client secret JSON file.' in /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php:171 Stack trace:

#0 /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php(189): Google_Client->setAuthConfig('{? "type": "se...')

#1 /var/www/vhosts/mydomain.com/httpdocs/test3.php(36): Google_Client->setAuthConfigFile('/var/www/vhosts...')

#2 {main} thrown in /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php on line 171

Can anyone suggest why my JSON key file might be failing?

Update: OK, I have been trying to get this working for 5 days now. Maybe it can work, but my faith in Google's documentation is bust. Google, if you're reading this, please sort out your docs, they are sloppy in the extreme. Test them out on people.

If anyone can provide a working example of the API Server-side Authorization demo (https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/), but using PHP instead of Python for Step 3, I will be eternally grateful. Thanks.

1条回答
疯言疯语
2楼-- · 2019-04-15 01:42

here is how I ended up doing this

$scope = 'https://www.googleapis.com/auth/analytics.readonly';
$jsonKey = json_decode('[json file content here]', true);
// OR json key file path
// $jsonKeyFilePath = '/full/path/to/service-account.json';

$client = new \Google_Client();
$client->setScopes([$scope]);
$client->setAuthConfig($jsonKey);

// OR use json file
// $client->setAuthConfigFile($jsonKeyFilePath);

$client->useApplicationDefaultCredentials();
$client->fetchAccessTokenWithAssertion();

echo $client->getAccessToken();

In google api client v2 there were some updates https://github.com/google/google-api-php-client/blob/master/UPGRADING.md

that as of Sep 2016 are not yet updated in the docs (which contains howto for v1) https://developers.google.com/api-client-library/php/auth/service-accounts

查看更多
登录 后发表回答