Getting 403 error when accessing Google My Business API through Service Account

3.4k views Asked by At

I am working on a project where we want to collect data on GMB performance through the GMB API. This entails capturing many of the reportInsights results. We are not creating or updating any records for this account. I tried the Oauth2 approach however, that required me to provide 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 a service account is the best approach and I have created that credential in the Google API console.

I can create credentials, however, when I run 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 did include a valid API key from the Google API console but I get 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 thrown from the last line. Any help is appreciated.

2

There are 2 answers

12
James On

Try changing your code to the following

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 credentials from JSON file

from oauth2client.client import AccessTokenCredentials
credentials = AccessTokenCredentials.from_json(credentials_file)
http = httplib2.Http()
http = credentials.authorize(http)
2
stupidbodo On

The problem is with the discovery service url. It seems that private apis can't be accessed via discovery api service (even when you use apikeys). As such, the resolution is to change discovery service url to a valid json file displaying mybusiness service.

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