如何使用 PHP 运行 Google Embedded API 服务器端授权


How to run Google Embed API Server-side Authorization with PHP?

我正在尝试运行Google Embedded 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),为其启用了分析 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'); ,出现以下错误:

致命错误:未捕获的异常"Google_Exception",并显示消息 "无效的客户端机密 JSON 文件。" /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->setAuthConfig('{?"类型": "SE...")

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

#2 {main} 扔在/var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php 在第 171 行

谁能建议为什么我的 JSON 密钥文件可能失败?

更新:好的,我一直在尝试让它工作 5 天了。也许它可以工作,但我对谷歌文档的信心是崩溃的。谷歌,如果你正在阅读这篇文章,请整理你的文档,它们非常草率。在人身上测试它们。

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

这是我最终这样做的方式

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

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

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