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

GMB API - impersonate a user account

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

I want to know if it possible to access user data after being authorized by him from my server using scripts like cronjob etc'

The access has to be without the actual user online, I've tried several codes but so far I couldn't succeed,

It would be nice if you can provide an code example how it should be, I'm using PHP but I can also work with python.

 

thanks in advance for your help,

Shlomi.

1 Expert replyverified_user

Re: GMB API - impersonate a user account

Google Employee
# 2
Google Employee

Hi @Shlomi S

 

If you are using a service account for authentication, please note that a one-time user consent is required for a service account to be able to access the Google My Business account and location data on behalf of a particular end user.

 

You can request user consent using OAuth 2.0 installed applications flow, OAuth 2.0 web server applications flow. This user consent process is the same for the authorization of your application during the OAuth 2.0 installed application flow. When this token is provided and you are making API calls, you will be able to view and manage all the existing locations within your business Google Account. You can perform this user consent process with a single set of OAuth 2.0 credentials from the whitelisted project for different Google Accounts individually to access the account and location data in a specific Google Account.

 

In addition, when using the OAuth 2.0 client ID for authorization, you should store the refresh token for future use and use the access token to access the API. Once the access token expires, the application should use the refresh token to obtain a new one. This way, your application will always be able to request a new access token when necessary. This process requires a user to manually authorize the application only once.

 

Please check out this Accepted Solution to set up your service account application for different Google Accounts.

 

Thanks,

Shalini, The Google My Business API team

GMB API - impersonate a user account

[ Edited ]
Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Thank you @Shalini S

 

I successfully got the permission required from the user for a service account and now he can see my app in his authorized apps list,

I still can't see his business locations when I'm trying to view the accounts list or  the locations list:

 

gmbsc1.jpg 

 

Part of my code:

 

$user_to_impersonate='<customer email address>';
$client->setAccessToken($_SESSION['access_token']);
$client->setSubject($user_to_impersonate);
$mybusinessService=newGoogle_Service_Mybusiness($client);
$accounts=$mybusinessService->accounts;
$accountsList=$accounts->listAccounts()->getAccounts();
$account=$accountsList[0];
$accountName=$account->name;
echo $accountName;
$locations=$mybusinessService->accounts_locations;
$locationsList=$locations->listAccountsLocations($accountName)->getLocations();
$location=$locationsList[0];
$locationName=$location->locationName;
echo $locationName;
 
What's wrong ?
 


 

 

Re: GMB API - impersonate a user account

Google Employee
# 4
Google Employee

Hi @Shlomi S,

 

Are you able to list all accounts via the OAuth 2.0 Playground by following the Make a simple HTTP request guide?

 

Thanks,

The Google My Business API Team

GMB API - impersonate a user account

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭

I can't use the OAuth playground since it's using its own project which is 'google.com' and not my authorized project,

Anyway the answer is:
 

{
 
"error": {
   
"status": "PERMISSION_DENIED",
   
"message": "Google My Business API has not been used in project google.com:oauth-2-playground before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/mybusiness.googleapis.com/overview?project=google.com... then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.",
   
"code": 403,
   
"details": [
     
{
       
"@type": "type.googleapis.com/google.rpc.Help",
       
"links": [
         
{
           
"url": "https://console.developers.google.com/apis/api/mybusiness.googleapis.com/overview?project=google.com...",
           
"description": "Google developers console API activation"
         
}
       
]
     
}
   
]
 
}
}

Re: GMB API - impersonate a user account

Google Employee
# 6
Google Employee

Hi @Shlomi S,

 

The common cause for not being able to find the Google My Business API in Google Developers Console is your Google Account may not have been submitted in the access request form as an email address that will administer the Project ID.

 

We’ll reach out to you privately in a separate email thread for more details.

 

Thanks,

The Google My Business API Team

GMB API - impersonate a user account

Visitor ✭ ✭ ✭
# 7
Visitor ✭ ✭ ✭

Hi @Shalini S,

 

I managed to get a results using the OAuth 2.0 playground and get the list of the account using a simple http request,

However I can see the account that I'm currently logged in with and I want to get access to the account information even if the user isn't online at the moment, I believe I need to setup a service account but I can find a proper documentation how I can do that,

Be aware, I already requested that a one-time user consent and my app is listed in his authorized app,

All I need as an example or a proper guide to to setup a service account using the php api to access the google my business data of someone who isn't currently logged in.

 

Thank you in advance,

Shlomi.

Re: GMB API - impersonate a user account

Google Employee
# 8
Google Employee

Hi @Shlomi S,

 

I suggest you check out this Accepted Solution for setting up a service account application with the PHP client library.

 

I hope this helps!

 

Thanks,

Shalini, The Google My Business API team