首页 > 解决方案 > Google MyBusiness API PHP 位置

问题描述

这是我第一次遇到 Google MyBusiness API。我试图检索帐户位置,但我一无所获。

我没有直接访问帐户的权限,我的客户通过 json 文件让我访问了 API。

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/plus.business.manage');

$service = new Google_Service_MyBusiness($client);
$accounts = $service->accounts->listAccounts();

而且我没有任何错误,只是Google_Service_MyBusiness_ListAccountsResponse反对,然后放弃foreach该对象,我可以获得帐户信息:

object(Google_Service_MyBusiness_Account)[94]
  protected 'internal_gapi_mappings' => 
    array (size=0)
      empty
  public 'accountName' => null
  public 'accountNumber' => null
  public 'name' => string 'accounts/XXXXXXXXXXXXXXXXXXXXX' (length=30)
  protected 'organizationInfoType' => string 'Google_Service_MyBusiness_OrganizationInfo' (length=42)
  protected 'organizationInfoDataType' => string '' (length=0)
  public 'permissionLevel' => null
  public 'profilePhotoUrl' => string '.../photo.jpg' (length=94)
  public 'role' => null
  protected 'stateType' => string 'Google_Service_MyBusiness_AccountState' (length=38)
  protected 'stateDataType' => string '' (length=0)
  public 'type' => string 'PERSONAL' (length=8)
  protected 'modelData' => 
    array (size=0)
      empty
  protected 'processed' => 
    array (size=0)
      empty
  public 'state' => 
    object(Google_Service_MyBusiness_AccountState)[84]
      protected 'internal_gapi_mappings' => 
        array (size=0)
          empty
      public 'status' => string 'UNVERIFIED' (length=10)
      protected 'modelData' => 
        array (size=0)
          empty
      protected 'processed' => 
        array (size=0)
          empty

现在当我尝试:

$name = $account->getName(); //accounts/XXXXXXXXXXXXXXXXXXXXX
$service->accounts_locations->listAccountsLocations($name);

我得到几乎空的Google_Service_MyBusiness_ListLocationsResponse对象:

object(Google_Service_MyBusiness_ListLocationsResponse)[88]
  protected 'collection_key' => string 'locations' (length=9)
  protected 'internal_gapi_mappings' => 
    array (size=0)
      empty
  protected 'locationsType' => string 'Google_Service_MyBusiness_Location' (length=34)
  protected 'locationsDataType' => string 'array' (length=5)
  public 'nextPageToken' => null
  public 'totalSize' => null
  protected 'modelData' => 
    array (size=0)
      empty
  protected 'processed' => 
    array (size=0)
      empty

怎么了?我是在做错什么,还是我有错误的凭据(?)。我如何检查/询问客户他给了我正确的访问权限。他可以在他的面板中检查帐户名称吗?

标签: phpgoogle-apigoogle-api-php-clientservice-accountsgoogle-my-business-api

解决方案


您似乎正在尝试使用服务帐户。根据文档基本设置,您需要创建一个 Oauth2 客户端并使用它来登录可以访问我的企业帐户的帐户。

根据文档,Google MyBusiness API 似乎不支持服务帐户。


推荐阅读