Search code examples
phpgoogle-apigoogle-api-php-clientservice-accountsgoogle-my-business-api

Google MyBusiness API PHP Locations


its my first Google MyBusiness API encounter. Im trying to retrieve account locations but ive got nothing.

I dont have direct access for account, my client gave me access to API by json file.

$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();

and i don't have any error, just Google_Service_MyBusiness_ListAccountsResponse object, then giving foreach on that object and I can get account informations:

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

Now when i try:

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

i get almost empty Google_Service_MyBusiness_ListLocationsResponse object:

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

What is wrong? Am I doing something wrong, or i have wrong credentials(?). How can I check/ask client that he gave me correct access. Can he check Account name in his panel or something?


Solution

  • You appear to be trying to use a service account. As per the doucmentation Basic setup you need to create an Oauth2 client and use that to login to an account that has access to the my business account.

    Service accounts do not appear to be supported by Google MyBusiness API as per the documentation.