Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 251
analyticsPierce Avatar asked Sep 07 '17 18:09

analyticsPierce


People also ask

How do I fix Google authorization error 403?

"code": 403, "message": "The user does not have sufficient permissions for file {fileId}." To fix this error, instruct the user to contact the file's owner and request edit access. You can also check user access levels in the metadata retrieved by files.

What is Google status Error 403?

In more human-friendly terms, the 403 Forbidden Error means that your website's server is telling Googlebot that it does not have permission to access the page or resource that you're trying to crawl.

How do I open a 403 Forbidden page?

Many times the 403 error is temporary, and a simple refresh might do the trick. Most browsers use Ctrl+R on Windows or Cmd+R on Mac to refresh, and also provide a Refresh button somewhere on the address bar. It doesn't fix the problem very often, but it takes just a second to try.


2 Answers

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"
like image 153
stupidbodo Avatar answered Nov 01 '22 17:11

stupidbodo


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)
like image 39
James Avatar answered Nov 01 '22 17:11

James