php - Google My Business API - 我设法显示所有评论
问题描述
经过大量的研究和实验,我能够在我的网站上显示所有谷歌评论,在这里发布这个问题的原因是,我第一次玩 API,我的想法很少。我不确定我的方法是正确的还是可以进一步改进?代码在安全方面是否也是安全的。
采取了以下步骤,您可能知道,我们必须做一些先决条件,我做了。
获得批准后,我通过 Google Oauth Playground 测试了 API,并成功获得
accoundId
了locationId
. (https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews)为了在网站上实施评论,我使用了 Google PHP 客户端库 ( https://github.com/googleapis/google-api-php-client )。
现在让我们进入主要部分,要获取我们需要在 URL 末尾添加“访问令牌”的所有结果。(https://mybusiness.googleapis.com/v4/accounts/102xxxxxxx/locations/733xxxxxxx/reviews?access_token=xxxxxxxxxx)
现在,问题是访问令牌在一小时后过期,为了克服这个问题,我生成了一个刷新令牌并使用以下代码。虽然我不确定,刷新令牌是否会过期?
<?php
// include your composer dependencies
require_once 'GoogleClientApi/vendor/autoload.php'; // or wherever autoload.php is located
$refreshToken = 'xxxxxxxxxxxx'; // generrated from https://developers.google.com/oauthplayground/
$name = "accounts/xxxxxxx/locations/xxxxxxxx"; // generrated from https://developers.google.com/oauthplayground/
//PHP Client Library
$client = new Google_Client();
$client->setClientId("xxxxxx"); // generated from Google Cloud Platform
$client->setClientSecret("xxxxx"); // generated from Google Cloud Platform
$client->refreshToken($refreshToken); // as set above in variable.
//Authorization Scopes
//$client->addScope("https://www.googleapis.com/auth/business.manage"); // Not needed probably.
$access_token = $client->getAccessToken(); // confused here...
$client->setAccessToken($access_token); // confused here..
$token = $access_token['access_token'];
$jsonDoc = file_get_contents("https://mybusiness.googleapis.com/v4/accounts/xxxxx/locations/xxxx/reviews?access_token=$token");
$array = json_decode($jsonDoc, true); // when true works as assoc array ?>
print_r($array) // output the JSON formatted reviews.
现在,我脑海中提出的问题:
- 我通过 Googe OAuth 游乐场生成的刷新令牌会过期吗?如果是,我是否必须通过 Playground 再次重新生成令牌并每次在文件中手动添加代码?
- 这两行我很困惑。以下代码在每次页面刷新时生成一个新的访问令牌这是正常流程吗?还是违反任何 Google 政策,或者我只是想多了?
$access_token = $client->getAccessToken(); // confused here...
$client->setAccessToken($access_token); // confused here..
- 我是否需要将刷新令牌或访问令牌存储在任何文件或数据库中?
解决方案
使用此代码您将遇到的问题是,每次刷新令牌过期时,您都需要再次对其进行授权。
如果您使用的是 oauth2 Playground,则刷新令牌可能会经常过期。由于操场仅用于测试目的。
您应该考虑做的是创建一个单用户系统。代码被授权,然后刷新令牌存储在以下示例中名为 token.json 的文件中
您需要授权一次,然后它将通过使用 token.json 文件中的刷新令牌请求一个新令牌来加载访问令牌。
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('PHP Quickstart');
$client->setScopes(Google_Service_Drive::BUSINESS.MANAGE);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
// Get the API client and construct the service object.
$client = getClient();
$service = new Google_Service_Drive($client);
我的一些笔记。
此代码将像在 Web 浏览器中一样运行命令,而不是在 Web 浏览器中运行。我建议您将您期望从 API 获得的响应缓存到数据库中。然后向用户显示来自数据库的响应。如果愿意,您可以将此代码设置为每小时作为 cron 作业运行,但实际上没有理由让它为每个用户实时运行。如果您尝试这样做,您可能会用完报价。缓存它是一个更好的选择。
由于谷歌最近所做的一些更改,如果您的应用程序处于测试阶段并且尚未经过验证,您的刷新令牌最多只能使用两周。之后,用户同意将被撤销,刷新令牌将过期,您将需要再次授权。因此,在进行生产之前,请务必申请验证。当你去的时候向谷歌解释它是一个单一的用户脚本。
我有一个关于验证过程的视频,可能会有所帮助。2021 年您需要了解的有关 Google 验证的信息。
推荐阅读
- azure - 带有 LUIS 的 qnamaker
- java - 我必须输入相同的字符串两次才能运行“else if”代码
- excel - 如何在 VBA 中将打开的 excel 文件设置为工作簿对象
- php - Wordpress Text Widget 用 html 注释标签替换 php 标签?
- sql - 访问 - 多个条件不起作用的第 3 个左连接
- c# - 当“真相源”具有通用多个操作时如何同步访问
- c# - 将对象转换为 PartialView 中的模型
- android - 在 PlaceAutocompleteFragment 中添加语音搜索
- r - 地块联合回报期
- node.js - 运行 .getAttribute() 命令时发生错误
: TypeError: _this2.client.assertion 不是函数