Use the My Business API in Python

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.

System requirements

The following are the system requirements needed to work with the Google My Business API in Python. For more details, see Basic setup.

Install the required libraries

The easiest way to use the Google My Business API in Python is with the Google APIs Python library, and the OAuth2 Python library To install these libraries, run the following:

$ 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:

  1. Open the Credentials page in the API Console.
  2. Click the name of a client ID to view that ID.
  3. Click Download JSON.
  4. Create a new file called client_secrets.json in 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 Python file.

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"][0]["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)