Google My Business API to Obtain Location Reviews & Action on Reviews.
What will I be covering in this medium story?
- How to get My Business API Access from google.
- Setup/Create OAuth 2.0 credentials.
- Get Authorization Code & Access token from Google OAuth 2.0 playground.
- Get account/locations associated with My Business API.
- Get access token in python.
- List Reviews, Add/Update comment on the review, Delete Reviews.
How to get My Business API Access from google.
In Order to enable the Google My Business API , fill out the form. Google team provides access to this API in 2–3 working days. Enable this API on getting access from google.
Setup/Create OAuth 2.0 credentials.
Setup OAuth Consent Screen and Create Credentials using this link.
Get Access token from Google OAuth 2.0 playground.
- Go to OAuth 2.0 Playground
- Click the gear icon to open the configuration options and make the following changes:
-> Set OAuth flow to Client-side.
-> Select Use your own OAuth credentials.
-> Paste in your OAuth client ID.
-> Close the configuration options.
- Under “Step 1 — Select & authorize APIs,” paste the following scope for the Google My Business API into the Input your own scopes field:
- Click Authorize APIs.
- Click Accept when prompted.
Get account/locations associated with My Business API.
- Under “Step 2 — Configure request to API,” paste the following URI into the Request URI field:
- Take the accountId from the response.
You can find the locationId with the above request, substituting your accountId for the number you see above.
Get access token in python.
- Install Libraries & Download client_secrets.json.
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlibDownload json:
- Copy below code in order to get an access token using google built-in python Installed App Flow and local server.
from __future__ import print_function
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/business.manage']
CLIENT_SECRETS_FILE = 'client_secrets.json'def main():
creds = None
# The file token.json stores the user's access and refresh tokens, and is created automatically when the authorization flow completes for the first time.
creds = Credentials.from_authorized_user_file('token.json', SCOPES) #If there are no (valid)credentials available,let the user logIn.if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
flow = InstalledAppFlow.from_client_secrets_file(
creds = flow.run_local_server(port=0, ,access_type='offline', prompt='consent') # Save the credentials for the next run
with open('token.json', 'w') as token:
if __name__ == '__main__':
If you open the token.json file, you can see the access token and refresh token in the file. The refresh token allows you to refresh the access token whenever you run this file.
List Reviews, Add/Update comment on the review, Delete Reviews(API doc).
Pass the accountId and locationId which you get from the above steps.
- List Reviews of Locations:
- Get Reviews using ReviewId:
- Reply to a review:
comment: "Thank you for visiting our business!"
The comment is JSON data which you need to pass in the request call.
- Delete a Review
I really hope this helped you! I had a bit of difficulty working out which numbers I needed and how to obtain them. There is so much documentation it can be easy to get overwhelmed. So I place it all in one article hopefully to make things more streamlined for people in the future.
Thank you for reading :)
My Business API.
QuickStart Python (Access token).