How to "build" a service[ Edited ]
July 2016 - last edited July 2016 by Yash S
I'm trying to build a service that once a day gets all the new reviews of a +500 locations account using Python. I am trying to install it on a server to "cron it" once a day but, if I connect with another Google API, for instance, drive:
scopes = ['https://www.googleapis.com/auth/drive.metadata.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('service-account.json', scopes)
http_auth = credentials.authorize(Http())
drive = build('drive', 'v3', http=http_auth)
I can "build" the service object.
How could I do something similar with My Business API?
Re: How to "build" a service[ Edited ]
July 2016 - last edited August 2016 by Terry W
Hi @David C,
You should use the Google API Client Library for Python to handle all requests since all methods in the Google My Business API are included in the discovery document for the API. Please use the client libraries we provide when interacting with Google's OAuth 2.0 endpoints for the security implications of getting the implementation correct.
Please note that a one-time user consent is required for a service account to be able to access the Google My Business account and location data on behalf of a particular end user. The end users will have to log in with their Google Accounts to manually authorize the application during the OAuth 2.0 installed applications flow or using OAuth 2.0 Playground. This process needs to be done only once. When you prepare to make authorized API calls using the service account, you specify the user to impersonate by specifying the email address of the user account for access to their Google My Business data. If the end users have manually authorized the application, you should be able to access their Google My Business data. If the end users later want to revoke the application’s access to their Google My Business data, they can remove the authorized app from the Apps connected to your account page of their accounts.
Here is a code snippet for the Google API Client Library for Python that performs service account authentication:
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
client_email = 'firstname.lastname@example.org'
scopes = ['https://www.googleapis.com/auth/plus.business.manage']
credentials = ServiceAccountCredentials.from_p12_keyfile(
client_email, '/path/to/keyfile.p12', scopes=scopes)
delegated_credentials = credentials.create_delegated('email@example.com')
http = Http()
response, content = http.request("https://mybusiness.googleapis.com/v3/accounts", "GET")
We hope this solves your problem.
The Google My Business API team