Aside from the languages that we offer client libraries for, the Google My Business API can also be used in many other languages. This page provides information and an example of how to use the API in Python.
Before you work with the Google My Business API, you need to register your application and obtain OAuth 2.0 credentials. See Basic setup for details on how to work with the Google My Business API.
The following are the system requirements needed to work with the Google My Business API in Python. For more details, see Basic setup.
- Operating systems:
- Mac OS X
- Python 2.7, 3.4 or higher
Install the required libraries
$ pip install --upgrade google-api-python-client $ pip install oauth2client
Get your client secrets
An OAuth 2.0 client ID must be created before you proceed with the following steps:
- Open the Credentials page in the API Console.
- Click the name of a client ID to view that ID.
- Click Download JSON.
- Create a new file called
client_secrets.jsonin the same directory as your Python file and add to it the contents of the JSON file that you downloaded in step 3.
Download the discovery document
Go to the Samples page, right
click Download discovery document, and select Save Link As. Then, save
the file as
myBusiness_discovery.json in the same directory as your
Call the API
Use the built in
sample_tools utility of the Google APIs Python client
to build an API service from the discovery document that you downloaded,
and authenticate the user with OAuth. Now the user has access to their
My Business account.
The following example returns a list of accounts accessible to the user and the locations contained in the first account:
from googleapiclient import sample_tools from googleapiclient.http import build_http discovery_doc = "gmb_discovery.json" def main(argv): # Use the discovery doc to build a service that we can use to make # MyBusiness API calls, and authenticate the user so we can access their # account service, flags = sample_tools.init(argv, "mybusiness", "v4", __doc__, __file__, scope="https://www.googleapis.com/auth/business.manage", discovery_filename=discovery_doc) # Get the list of accounts the authenticated user has access to output = service.accounts().list().execute() print("List of Accounts:\n") print(json.dumps(output, indent=2) + "\n") firstAccount = output["accounts"]["name"] # Get the list of locations for the first account in the list print("List of Locations for Account " + firstAccount) locationsList = service.accounts().locations().list(parent=firstAccount).execute() print(json.dumps(locationsList, indent=2)) if __name__ == "__main__": main(sys.argv)