AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

My Business
2.4K members online now
2.4K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

How to get ALL locations

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi Guys,

 

Just need some help in simply receiving a json feed from my Google My Business locations. I have tried using the php client and Mybusiness class to no avail. I do not wish to edit any details simply want to integrate GMB with Wordpress by getting the json feed converting it to a CSV and uploading it to my store locator plugin in wordpress. Any assistance on this matter is deeply appreciated ! 

 

Bass

2 Expert replyverified_user

Re: How to get ALL locations

Google Employee
# 2
Google Employee

Hi Bass from @HSW Marketing H,

 

You can use the PHP client library to send a GET request via the Google My Business API to list all of the locations for the specified account. At that point, you can use the json_encode function and pass the returned Location objects as an argument to get the JSON representation of all locations, or you can use the fputcsv function and pass the returned Location objects as an argument to directly write the fields to a CSV file.

 

I suppose you have gotten through the OAuth 2.0 Authorization, then you can follow the example in the below code snippet for listing the locations for the specified account with the PHP client library:

/************************************************

 We are going to create the Google My Business API

 service, and query it.

************************************************/

$mybusinessService = new Google_Service_Mybusiness($client);

 

$accounts = $mybusinessService->accounts;

// Lists all accounts for the authenticated user.

try {

 $accountsList = $accounts->listAccounts()->getAccounts();

} catch (Exception $e) {

 print "Caught exception: " . $e->getMessage() . "\n";

}

 

$locations = $mybusinessService->accounts_locations;

 

// For testing purposes, selects the very first account in the accounts array

$account = $accountsList[0];

 

// Lists all of the locations for the specified account.

try {

 $locationsList = $locations->listAccountsLocations($account->name)->getLocations();

} catch (Exception $e) {

 print "Caught exception: " . $e->getMessage() . "\n";

}

 

I hope this helps.

 

Thanks, 

Terry

How to get ALL locations

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

hey where you find "$client"?

How to get ALL locations

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭

Hi

What is the value of the $client variable?

Thanks

Re: How to get ALL locations

Google Employee
# 5
Google Employee

Hi @Sébastien J,

 

Please checkout the basic example shown in this document for the $client value.

$client = new Google_Client();

 

Thanks,

The Google My Business API team

Re: How to get ALL locations

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭

Thanks.

 

I did like that :

 

 

require_once em_misc::getSpecifPath().'_vendors/google-api-php-client/vendor/autoload.php';
require_once em_misc::getSpecifPath().'_vendors/google-api-php-client/vendor/google/apiclient-services/src/Google/Service/MyBusiness.php';

putenv('GOOGLE_APPLICATION_CREDENTIALS='.em_misc::getSpecifPath().'_vendors/gmb.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/plus.business.manage');

$mybusiness = new Google_Service_Mybusiness($client);
$accounts = $mybusiness->accounts;

try {
	$accountsList = $accounts->listAccounts()->getAccounts();
} catch (Exception $e) {
	print "Caught exception: ".$e->getMessage()."\n";
}

$locations = $mybusiness->accounts_locations;

$account = $accountsList[0];

try {
	$locationsList = $locations->listAccountsLocations($account->name)->getLocations();
} catch (Exception $e) {
	print "Caught exception: " . $e->getMessage() . "\n";
}

But $locationsList is empty array.

 

Thanks for help.

 

Re: How to get ALL locations

Google Employee
# 7
Google Employee

Hi @Sébastien J,

 

Please confirm if you were able to get a list of location of your account using the OAuth playground?

 

Since the PHP client library doesn’t currently support the display of detailed error messages, you should follow the Make a simple HTTP request step-by-step guide on the Google Developers site to learn how to use the OAuth 2.0 Playground for troubleshooting errors with detailed error messages when sending requests with data via the Google My Business API.

 

Thanks,

The Google My Business API team

How to get ALL locations

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭

Hi,

Indeed it works when I use OAuth playground with oauth but not with service account.

I would like to use the services account.

Re: How to get ALL locations

Google Employee
# 9
Google Employee

Hi @Sébastien J,

 

Following are the required steps you should take for the service account application to work with the Google My Business API:

  • You should make sure that you created a service account in your whitelisted project in Google API Console to access the API.
  • You should request user consent at least once for each Google Account you need to access the Google My Business data from. The end users will have to log in with their Google Accounts to manually authorize the application using OAuth 2.0 Authorization. They need to perform this process only once with an OAuth 2.0 client ID during the OAuth 2.0 installed application flow, OAuth 2.0 web server application flow (your other OAuth 2.0 client ID web app) or via OAuth 2.0 Playground. This process should be performed prior to using the service account application with its public/private key pair credentials and not while running it. You can verify or remove the authorized app from the Apps connected to your account page of each Google Account you requested user consent for.
  • When you prepare to make authorized API calls using the service account to access a particular end user’s Google My Business data, you specify the user to impersonate by specifying the email address of the Google Account that you have previously given user consent for.

You should be able to get your service account application to work after you’ve applied the above steps. Please let me know if you have further questions.

 

Thanks,

The Google My Business API team