php - 如何使用 PHP 运行 Google Embed API 服务器端授权?

标签 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 key 数据请求访问 token )。

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

我创建了一个服务帐户 ( https://console.developers.google.com ),为其启用了 Analytics API,下载了 JSON key 文件,并将其复制到我的 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'); ,
出现以下错误:

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



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

更新:好的,我已经尝试让它工作 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();

在 google api client 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

关于php - 如何使用 PHP 运行 Google Embed API 服务器端授权?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35793674/

相关文章:

google-chrome-extension - Chrome 扩展程序 background.html 中的 Google Analytics 跟踪

javascript - gapi.client未定义-Firefox附加SDK

php - 当我提交表单时,我的数据库不会插入我的记录

php - 图片无法从 url 加载

php - PHP警告: Illegal string offset & Invalid argument supplied for foreach() [duplicate]

json - Google Analytics Reporting API 返回空对象 - NodeJs

web - 为什么我的 Google Analytics 唯一用户计数与数据不匹配?

google-analytics - 使用针对产品 SKU 的导入数据提供的自定义指标表现得很奇怪

youtube - Google Youtube API nextPageToken

php - 动态添加/删除新的表单输入