如何使用PHP运行Google嵌入式API服务器端授权?

时间:2016-03-04 10:28:42

标签: php google-analytics google-api google-analytics-api google-api-php-client

我正在尝试运行Google Embed API服务器端授权演示,此处为:https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/,但在步骤3中使用PHP而不是Python(使用JSON密钥数据来请求访问令牌)。

我已安装Google API PHP客户端:https://github.com/google/google-api-php-client

我创建了一个服务帐户(https://console.developers.google.com),为其启用了Analytics API,下载了JSON密钥文件,并将其复制到我的Web服务器(以下是Web根级别的安全性)。

我已在Google Analytics帐户中注册了服务帐户(仅读取和分析)。

我正在尝试使用替换PHP代码来运行演示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());
?>

运行演示(在名为test3.php的文件中,使用PHP代码而不是Python代码)在此行中失败, $client->setAuthConfigFile('client_secrets.json');, 出现以下错误:

  

致命错误:未捕获的异常&#39; Google_Exception&#39;与消息   &#39;无效的客户端密钥JSON文件。&#39;在   /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php:171   堆栈跟踪:

     

#0 /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php(189):   Google_Client-&gt; setAuthConfig(&#39; {?&#34; type&#34;:&#34; se ...&#39;)

     

#1 /var/www/vhosts/mydomain.com/httpdocs/test3.php(36):Google_Client-&gt; setAuthConfigFile(&#39; / var / www / vhosts ...&#39;)< / p>      在/var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php中抛出

#2 {main}   在第171行

有人可以建议为什么我的JSON密钥文件可能会失败吗?

更新:好的,我一直试图让这个工作5天了。也许它可以奏效,但我对Google的文档的信心已经破灭。谷歌,如果您正在阅读本文,请整理您的文档,它们是极端的马虎。对他们进行测试。

如果有人可以提供API服务器端授权演示(https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/)的工作示例,但是在步骤3中使用PHP而不是Python,我将永远感激不尽。感谢。

1 个答案:

答案 0 :(得分:2)

这是我最终如何做到这一点

$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();

在google api client v2中有一些更新 https://github.com/google/google-api-php-client/blob/master/UPGRADING.md

截至2016年9月尚未在文档中更新(其中包含v1的howto) https://developers.google.com/api-client-library/php/auth/service-accounts