【问题标题】:How to get Business Locations (and Reviews) via Service Account authentication如何通过服务帐户身份验证获取营业地点(和评论)
【发布时间】: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 guideOAuth 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


【解决方案1】:

最后,在我第一次授予(我的)应用程序权限时,我使用 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(' ###### ')

现在我获得了应用程序所需的所有数据。

【讨论】:

  • 您可以将此标记为答案。它工作正常
  • 太好了,终于有东西了!因此,服务帐户似乎不适用于 My Business API。
  • Google 有时不会通过弹出对话框再次征求您的同意吗?因为这些令牌应该只在一定时间内有效,并且当它们失效时,您要么必须手动生成它们,要么让 Google 显示同意对话框,让您再次授予必要的权限。如果我遗漏了什么,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 2014-11-02
  • 2016-05-23
  • 1970-01-01
  • 2017-04-02
  • 1970-01-01
  • 2017-01-15
相关资源
最近更新 更多