Work with account data

This tutorial shows you how to work with account data. The Google My Business API provides you with the ability to do the following:

  • List all accounts.
  • Return a specific account.
  • Update an account.

Before you begin

Before you use the Google My Business API, you need to register your application and obtain OAuth 2.0 credentials.

For details on how to get started with the Google My Business API, see Basic setup.

List all accounts

A great way to validate the accounts associated with an authenticated user is to list all their accounts. Use the accounts.list API to list all accounts associated with a user.

To list all the accounts for an authenticated user, use the following:

HTTP
$ GET https://mybusiness.googleapis.com/v4/accounts
Java

The following function uses Mybusiness.Accounts.List to return a list of accounts.

/**
 * Returns a list of accounts.
 * @return List A list of accounts.
 * @throws Exception
 */
public static List listAccounts() throws Exception {
  Mybusiness.Accounts.List accountsList = mybusiness.accounts().list();
  ListAccountsResponse response = accountsList.execute();
  List accounts = response.getAccounts();

  for (Account account : accounts) {
    System.out.println(account.toPrettyString());
  }
  return accounts;
}

Return a specific account

Sometimes, you only want to see information about a specific account for an authenticated user. Use the accounts.get API to return a specific account and review additional account details.

To return a specific account by name, use the following:

HTTP
$ GET https://mybusiness.googleapis.com/v4/accounts/{accountId}
Java

This function uses Mybusiness.Accounts.Get to return the data for a single account.

/**
 * Demonstrates getting an account by name.
 * @param accountName The name (resource path) of the account to retrieve.
 * @return Account The requested account.
 */
private static Account getAccount(String accountName) throws Exception {
  Mybusiness.Accounts.Get account =
      mybusiness.accounts().get(accountName);
  Account response = account.execute();

  return response;
}

Additional data

The Java Client Library gives you access to additional field data for account instances. Use the following methods to return additional data about the account:

  • getAccountName()
  • getName()
  • getRole()
  • getState()
  • getType()

Update an account

Changing an account name is a fundamental administrative task. Use the accounts.update API to alter the account name.

To update a specific account by name, use the following:

HTTP
$ PUT https://mybusiness.googleapis.com/v4/accounts/{accountId}

{
  "account": {
    "accountName": "Anne Droyd"
  },
  "languageCode": "en",
  "validateOnly": "true"
}
Java

This function uses Mybusiness.accounts.update to update the accountName field for a business account.

/*
 * Updates a field for a business account.
 * @param accountName The name (resource path) of the account to update.
 * @param newAccountName A string containing the updated value.
 * @throws IOException
 */
private static void updateAccount(String accountName, String newAccountName) throws IOException {
  Account updateAccount = new Account();
  updateAccount.setName(accountName);
  updateAccount.set("accountName", newAccountName);

  Mybusiness.Accounts.Update accountUpdate =
      mybusiness.accounts().update(accountName, updateAccount);

  accountUpdate.setLanguageCode("en");
  Account updatedAccount  = accountUpdate.execute();

  System.out.printf("Updated account:\n%s", updatedAccount.toPrettyString());
}

Restrictions

  • The only editable field for an account is accountName. Any other fields passed are ignored.
  • Only accounts of BUSINESS type can be updated in this manner.