![](/img/trans.png)
[英]How to get all the reviews within the date range in Google My Business Api?
[英]Google My Business API - I managed to display all reviews
经过大量的研究和实验,我能够在我的网站上显示所有的谷歌评论,在这里发布这个问题的原因是,我第一次玩 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.
现在,我想到的问题是:
$access_token = $client->getAccessToken(); // confused here...
$client->setAccessToken($access_token); // confused here..
使用此代码将遇到的问题是,每次刷新令牌到期时,您都需要再次对其进行授权。
如果您使用的是 oauth2 playground,则刷新令牌可能会经常过期。 由于 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 验证的信息。
需要刷新令牌才能再次重新生成访问令牌。 所以你必须存储刷新令牌。 访问令牌在一小时后过期,而刷新令牌永不过期,您可以在需要时从刷新令牌中检索访问令牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.