Getting 403 error while accessing the Google My Business API through a service account

I am working on a project where we want to collect GMB performance data through the GMB API. This entails capturing many report results. We do not create or update any records for this account. However, I tried the Oauth2 approach, which required me to grant permission, and since we are not accessing or updating any user data, I would like to avoid Oauth.

From the documentation and for this use case, I believe that a service account is the best approach and I created these credentials in the Google API console.

I can create credentials, however, when I start the process, I get the following error:

googleapiclient.errors.HttpError: <HttpError 403 when requesting https://mybusiness.googleapis.com/$discovery/rest?version=v3 returned "The request is missing a valid API key."> 

This seems odd since I have a valid set of service account credentials. I have included a valid API key from the Google API console, but I am getting the same error.

Here is my Python code:

 import os import httplib2 import json import argparse import apiclient.discovery from oauth2client.service_account import ServiceAccountCredentials from apiclient.discovery import build api_name = 'mybusiness' api_version = 'v3' api_key = '<my valid api key from google api console that has permission for this GMB project>' discovery_uri = 'https://mybusiness.googleapis.com/$discovery/rest?version={}'.format(api_version) flow_scope='https://www.googleapis.com/auth/plus.business.manage' credentials_file = '/Google_My_Business-service_account.json' # the service account credentials from the Google API console that have permission for this GMB project credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials_file, scopes=flow_scope) print("credentials: ", credentials) http = credentials.authorize(httplib2.Http()) print("http: ", http) # Build the service object service = build(api_name, api_version, http=http, developerKey=api_key, discoveryServiceUrl=discovery_uri) 

The error is displayed from the last line. Any help is appreciated.

+7
python google-oauth google-my-business-api
source share
2 answers

Try changing the code to the next

 from oauth2client.client import AccessTokenCredentials credentials = AccessTokenCredentials('<an access token>', 'my-user-agent/1.0') http = httplib2.Http() http = credentials.authorize(http) 

Then, if that works, try the following to get the credentials from the JSON file

 from oauth2client.client import AccessTokenCredentials credentials = AccessTokenCredentials.from_json(credentials_file) http = httplib2.Http() http = credentials.authorize(http) 
+1
source share

The problem is with the discovery service URL. It seems that the private apis cannot be accessed through the opening api service (even if you use apikeys). Therefore, the resolution is to change the URL of the search service in a valid json file displaying the mybusiness service.

 discovery_uri = "https://developers.google.com/my-business/samples/mybusiness_google_rest_v3p3.json" 
0
source share

All Articles