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.
Try changing your code to the following
Then if that works try the following to get credentials from JSON file