【发布时间】:2019-01-29 02:04:23
【问题描述】:
当我使用"Service Account" authentication 时,我无法在我的代码下从我的企业获取位置列表(PHP 使用“Google APIs Client Library for PHP”和“Google_Service_MyBusiness”类),API 返回一个空的位置列表.
我已经有了Prerequisites 并且做了Basic setup,顺便说一句,我在 OAuth Playground 上获得了成功的信息,在特定的 AccountId 下,例如:1111111111,由“OAuth Playground”上的另一个响应提供”。 (PS:我用我的“PERSONAL”和“LOCATION_GROUP”账户进行了测试,都成功了)。
但是当我尝试通过服务器帐户身份验证对我的代码执行此操作时,我无法获取所有信息,只有返回另一个 AccoundId 的帐户数据,例如:2222222222,与我在 OAuth Playground 上获得的帐户不同.
我在 OAuth Playground 上进行了身份验证过程,使用的是我创建“服务帐户”的同一个项目,顺便说一下,这个“服务帐户”的权限是 OWNER。
以前,我在 Google 我的商家中的公司角色是“SITE_MANAGER”,所以我看到 a forum answer 只有“MANAGER”级别/角色可以列出位置,所以我请求更改我的权限,但继续不是位置列表的成功。
所以,我看到另一个Google My Business support article 建议创建一个“位置组”并将我当前的“位置”放入该组中以便于处理,我这样做了,但再次没有成功。
我的代码很简单,基于Google guide、OAuth 2.0 for Server to Server Applications 和一些Forum Questions(顺便说一句,作者的问题与我的问题相同):
<?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);
}
}
}
我期望我的业务的位置(也是评论,但它是下一步),但我只得到空的位置列表。
【问题讨论】:
-
我只想说你的问题用 google API 解决了 3 天的麻烦。谢谢!
-
@Emoyaki 很高兴为您提供帮助!每一个,教一个\0/
标签: php google-my-business-api