如何使用PHP运行Google Embed API服务器端授权? [英] How to run Google Embed API Server-side Authorization with PHP?

查看:87
本文介绍了如何使用PHP运行Google Embed API服务器端授权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

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

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

我已经创建了一个服务帐户( https://console.developers.google.com ) ,为此启用了Analytics(分析)API,下载了JSON密钥文件,然后将其复制到了我的网络服务器(出于安全性原因,该级别低于网络根目录级别).

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).

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

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

我正在尝试使用可替换的PHP代码从此处运行演示 https://stackoverflow.com/a/32767845/1803239 :

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

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

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:

致命错误:未捕获的异常"Google_Exception",并显示消息 无效的客户端机密JSON文件."在 /var/www/vhosts/mydomain.com/google-api-php-client/src/Google/Client.php:171 堆栈跟踪:

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 ...')

#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 ...')

#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行

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

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

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

更新:好的,我已经尝试使它工作5天了.也许它可以工作,但是我对Google文档的信念不容乐观. Google,如果您正在阅读本文,请整理您的文档,因为它们极端草率.对人进行测试.

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.

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

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.

推荐答案

这是我最终完成此操作的方式

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

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

In google api client v2 there were some updates 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

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

这篇关于如何使用PHP运行Google Embed API服务器端授权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆