Sign up for email announcements to stay updated on the latest information about the GMB API.

Set up the Java client library

The Google My Business API Java client library provides improved language integration and simplified user authentication. These instructions cover using the Eclipse IDE, but the client library should work in whichever IDE you choose.

Before working 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 start working with the Google My Business API.

Note: These instructions assume a Unix-like environment.

Client library downloads

Visit Downloads and Samples to get the latest client libraries.

Add the library to an Eclipse project

  1. Open your project in Eclipse.
  2. In Package Explorer, right-click a project and select Build Path, then click Configure Build Path.
  3. Click Add External JARs and navigate to the folder containing the JAR file to add.
  4. Select the JAR file and click Open.
  5. Click OK to add the file to the project's build path.

Get your client secrets

Note: You will need to have created a Client ID before proceeding.

  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 directory src/main/resources/, and add the contents of the JSON file you downloaded in the previous step.

Implement the OAuth 2.0 authorization flow

This example demonstrates using the AuthorizationCodeFlow class to allow the application to access Google My Business account data. All of the code here is taken from the Google My Business API Java client sample application, and follows the scenario described in Using OAuth 2.0 for Installed Applications.

In the following code example:

  1. The main() function calls authorize() to get a credential.
  2. The authorize() function creates an InputStream to hold the client ID and secret, creates an instance of GoogleClientSecrets, and returns a new AuthorizationCodeInstalledApp instance (the credential).
  3. The main() function uses the credential to create a new instance of MyBusiness.Builder named mybusiness. Now the app is ready to make API calls using mybusiness.
package com.company;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;

import com.google.api.services.mybusiness.v4.MyBusiness;
import com.google.api.services.mybusiness.v4.model.ListAccountsResponse;

import java.io.InputStream;
import java.io.InputStreamReader;

import java.util.Collections;
import java.util.List;


public class Main {

    private static final String APPLICATION_NAME =
        "Google My Business API Java Quickstart";
    private static final java.io.File DATA_STORE_DIR =
        new java.io.File(System.getProperty("user.home"),
            ".store/mybusiness_sample");
    private static FileDataStoreFactory dataStoreFactory;
    private static HttpTransport httpTransport;
    private static final JsonFactory JSON_FACTORY =
        JacksonFactory.getDefaultInstance();
    private static MyBusiness mybusiness;

    /**
     * Demonstrates the authentication flow to use
     * with the Google My Business API Java client library.
     * @return AuthorizationCodeInstalledApp
     */
    private static Credential authorize() throws Exception {
        // Creates an InputStream to hold the client ID and secret.
        InputStream secrets = Main.class.getResourceAsStream("/client_secrets.json");

        // Prompts the user if no credential is found.
        if (secrets == null) {
            System.out.println(
                "Enter Client ID and Secret from Google API Console "
                    + "into google-my-business-api-sample/src/main/resources/client_secrets.json");
            System.exit(1);
        }

        // Uses the InputStream to create an instance of GoogleClientSecrets.
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(secrets));
        if (clientSecrets.getDetails().getClientId().startsWith("Enter")
            || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")) {
            System.out.println(
                "Enter Client ID and Secret from Google API Console "
                    + "into google-my-business-api-sample/src/main/resources/client_secrets.json");
            System.exit(1);
        }

        // Sets up the authorization code flow.
        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
            httpTransport, JSON_FACTORY, clientSecrets,
            Collections.singleton("https://www.googleapis.com/auth/business.manage"))
            .setDataStoreFactory(dataStoreFactory).build();
        // Returns the credential.
        return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
    }

    public static void main(String[] args) throws Exception {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);

        // Calls the authorize() function to get a credential.
        Credential credential = authorize();

        // Calls MyBusiness.Builder to create a new instance named 'mybusiness'.
        mybusiness = new MyBusiness.Builder(httpTransport, JSON_FACTORY, credential)
            .setApplicationName(APPLICATION_NAME).build();

        // Uses the 'mybusiness' instance to send an API call.
        MyBusiness.Accounts.List accountsList = mybusiness.accounts().list();
        ListAccountsResponse response = accountsList.execute();
        List accounts = response.getAccounts();
    }
}