Access to the Google My Business API without logging in (using a service account)

I want to access the locations associated with my account and their reviews, for this I use the google API for my business and I have access to it (it works on oAuthplayground).

Now I want to access the google my business api without logging in to my account, because I'm trying to get it to work with the service account. But so far no luck, please advice on how to do this. I included the G-package in the service account, and I also tried to provide access to the email (ID) of the service account to manage my business, but it remains in the Invited state, since there is no way to accept the invitation.

When I try to send a request using my account as a subject.

$client = new Google_Client(); $client->addScope('https://www.googleapis.com/auth/plus.business.manage'); $client->setAuthConfig(dirname(__FILE__) . '/Xyz Review API-service account.json'); $client->setSubject('xyz***** abc@gmail.com '); $business_service_class = new Google_Service_Mybusiness($client); $result_accounts = $business_service_class->accounts->listAccounts(); echo json_encode($result_accounts); exit; 

Answer: {"NextPageToken": null}

If I use the google account id as the email id in the subject, I get the following response.

 $client->setSubject(' xyz-review-service@xyz-review-api.iam.gserviceaccount.com '); 

Answer: Error 500 {"error": "unauthorized_client", "error_description": "Unauthorized client or scope in the request." }

If I do this completely wrong, please advise how to do it. Thanks.

+7
php google-api google-api-php-client google-my-business-api
source share
1 answer

I ran into an authentication problem for my internal service using google apis. Basically, there are two methods:

  • create a page to accept your google account access application
  • create certificate for application authentication with implicit approval

since I said that I use google api for an internal project, so the first option is out of the question (the service is not publicly available). Go to https://console.cloud.google.com and create a new project, then go to the "api manager", then "credentials", then create the "service credentials".

If you follow all these steps, you have a certificate with the extension .p12, this is your key to access the google api (remember that you need to include the key to access the specific google api that you want).

I am inserting an example extracted from my project, I am using the Google calendar, but the authentication for each service is the same.

  $client_email = ' xxxx@developer.gserviceaccount.com '; $private_key = file_get_contents(__DIR__ . '/../Resources/config/xxxx.p12'); $scopes = array('https://www.googleapis.com/auth/calendar'); $credentials = new \Google_Auth_AssertionCredentials( $client_email, $scopes, $private_key ); $this->client = new \Google_Client(); $this->client->setAssertionCredentials($credentials); 
+1
source share

All Articles