Jon*_*Dev 3 php google-my-business-api
当我使用“服务帐户”身份验证时,我无法在我的代码下从我的企业获取位置列表(PHP 使用“ Google APIs Client Library for PHP ”和“ Google_Service_MyBusiness ”类) ,API 返回空位置列表。
我已经具备了先决条件并进行了基本设置,顺便说一句,我在 OAuth Playground 上成功获得了信息,在特定的 AccountId 下,例如:1111111111,由“OAuth Playground”上的另一个响应提供。(PS:我用我的“PERSONAL”和“LOCATION_GROUP”帐户进行了测试,并且都取得了成功)。
但是,当我尝试通过服务器帐户身份验证对我的代码执行此操作时,我无法获取所有信息,只能获取返回另一个 AccoundId 的帐户数据,例如:2222222222,与我在 OAuth Playground 上获得的帐户不同。
我在 OAuth Playground 上进行了身份验证过程,使用的是我创建“服务帐户”的同一项目,顺便说一句,这个“服务帐户”的权限是所有者。
以前,我在 Google 我的商家中的公司角色是“SITE_MANAGER”,所以我看到一个论坛答案,其中只有“MANAGER”级别/角色可以列出位置,所以我请求更改我的权限,但仍然没有成功在地点列表上。
因此,我看到另一篇“Google我的商家”支持文章建议创建一个“位置组”并将我当前的“位置”放入该组中以方便处理,我这样做了,但又没有成功。
我的代码很简单,基于Google 指南、OAuth 2.0 for Server to Server Applications和一些论坛问题(顺便说一句,作者的问题与我有同样的问题):
<?php
putenv('GOOGLE_APPLICATION_CREDENTIALS=service-account-credentials.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
require_once('Google_Service_MyBusiness.php');
$mybusinessService = new Google_Service_MyBusiness($client);
$accounts = $mybusinessService->accounts;
$accountsList = $accounts->listAccounts()->getAccounts();
foreach ($accountsList as $accKey => $account) {
var_dump('$account->name', $account->name);
$locations = $mybusinessService->accounts_locations;
$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
var_dump('$locationsList', $locationsList);
// Final Goal of my Code
if (empty($locationsList)===false) {
foreach ($locationsList as $locKey => $location) {
$reviews = $mybusinessService->accounts_locations_reviews;
$listReviewsResponse = $reviews->listAccountsLocationsReviews($location->name);
$reviewsList = $listReviewsResponse->getReviews();
var_dump('$reviewsList', $reviewsList);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我期望我的企业位置(也是评论,但这是下一步),但我只得到空的位置列表。
最后,我第一次向(我的)应用程序授予权限时,使用 ClientId/ClientSecret 密钥以及之前在 Google OAuth 2 Playground 上收到的刷新令牌(而不是“服务帐户”身份验证方式)获得了成功:)
$client = new Google_Client();
$client->setClientId($clientId);
$client->setClientSecret($clientSecret);
$client->addScope("https://www.googleapis.com/auth/plus.business.manage");
$client->setSubject('my email user on GMB');
$client->refreshToken(' ###### ')
Run Code Online (Sandbox Code Playgroud)
现在我获得了应用程序所需的所有数据。