Cat playing the piano

How to get Google My Business API up and running with a Service Account in Google Cloud: A Step-by-Step Guide

Connecting a service account in Google Cloud with Google My Business might sound like trying to teach a cat to play the piano. But don’t worry, I got a step-by-step guide for you that makes it as easy as microwaving a cup of coffee (please don’t do that). So, let’s dive into this adventure together!

Step 1: Contact Google and Get Access to the API

First things first, we need to get access to the Google My Business API. This means we need to contact Google and politely ask for access. Follow these steps:

  1. Go to Google Cloud Console: Visit Google Cloud Console.
  2. Create a Project: If you don’t already have a project, create a new one. Give it a name that makes you smile, maybe ”My Supercool API Connection”.
  3. Enable the API: Go to the API library and search for ”Google My Business API”. Click ”Enable”.

Step 2: Create a Service Account

Now that we have the API enabled, we need a service account to authenticate our requests.

  1. Go to Service Accounts: In Google Cloud Console, navigate to ”IAM & Admin” > ”Service Accounts”.
  2. Create a New Service Account: Click ”Create Service Account” and fill in the details. Give it a name like ”API Master” or something equally fun.
  3. Grant Permissions: Give the service account the ”Editor” role so it can make necessary changes.

Step 3: Download Service Account Credentials

To use the service account, we need its credentials in JSON format.

  1. Download the JSON Key: After creating the service account, click on it and go to the ”Keys” tab. Click ”Add Key” > ”Create New Key” and select JSON. Download the key file and keep it safe.

Step 4: Retrieve an Access Token Using Service Account Credentials

With the service account JSON file, we can now retrieve an access token to authenticate our API calls. Here’s how you can do it using the official Google PHP SDK library.

Install the Google PHP SDK

First, install the Google PHP SDK and Guzzle client using Composer. If you don’t have Composer installed, you can download it from here.

composer require google/apiclient
composer require guzzlehttp/guzzle

Google SDK library is really helpful to authenticate, but does not support Google My Business, so then I will use Guzzle client to make those requests.

Retrieve an Access Token

Use the following PHP script to retrieve an access token:

<?php
require 'vendor/autoload.php';

use Google\Client;

$client = new Client();
$client->setAuthConfig('path/to/your/service-account-file.json');
$client->addScope('https://www.googleapis.com/auth/business.manage');

$accessToken = $client->fetchAccessTokenWithAssertion()['access_token'];

echo $accessToken;

Replace 'path/to/your/service-account-file.json' with the path to your downloaded JSON file. This script will print the access token, which you can then use in your API requests.

Step 5: Find Your Account ID and Invitations Endpoint

To do anything useful, we need our account ID and the invitations endpoint.

Get Account ID

Use the following PHP script to find your account ID:

<?php
require 'vendor/autoload.php';

use Google\Client;
use GuzzleHttp\Client as HttpClient;

$client = new Client();
$client->setAuthConfig('path/to/your/service-account-file.json');
$client->addScope('https://www.googleapis.com/auth/business.manage');

$accessToken = $client->fetchAccessTokenWithAssertion()['access_token'];

$httpClient = new HttpClient();
$response = $httpClient->request('GET', 'https://mybusinessaccountmanagement.googleapis.com/v1/accounts', [
    'headers' => [
        'Authorization' => 'Bearer ' . $accessToken,
    ],
]);

$accounts = json_decode($response->getBody()->getContents(), true);
print_r($accounts);

Find Invitations Endpoint

To accept invitations, use:

<?php
require 'vendor/autoload.php';

use Google\Client;
use GuzzleHttp\Client as HttpClient;

$client = new Client();
$client->setAuthConfig('path/to/your/service-account-file.json');
$client->addScope('https://www.googleapis.com/auth/business.manage');

$accessToken = $client->fetchAccessTokenWithAssertion()['access_token'];

$accountId = 'your-account-id'; // Replace with your actual account ID

$httpClient = new HttpClient();
$response = $httpClient->request('GET', "https://mybusinessaccountmanagement.googleapis.com/v1/accounts/$accountId/invitations", [
    'headers' => [
        'Authorization' => 'Bearer ' . $accessToken,
    ],
]);

$invitations = json_decode($response->getBody()->getContents(), true);
print_r($invitations);

Step 6: Accept Invitations

Once we’ve found the invitations, we need to accept them. Use the following PHP script:

<?php
require 'vendor/autoload.php';

use Google\Client;
use GuzzleHttp\Client as HttpClient;

$client = new Client();
$client->setAuthConfig('path/to/your/service-account-file.json');
$client->addScope('https://www.googleapis.com/auth/business.manage');

$accessToken = $client->fetchAccessTokenWithAssertion()['access_token'];

$accountId = 'your-account-id'; // Replace with your actual account ID
$invitationId = 'your-invitation-id'; // Replace with your actual invitation ID

$httpClient = new HttpClient();
$response = $httpClient->request('POST', "https://mybusinessaccountmanagement.googleapis.com/v1/accounts/$accountId/invitations/$invitationId:accept", [
    'headers' => [
        'Authorization' => 'Bearer ' . $accessToken,
    ],
]);

echo $response->getStatusCode();

Step 7: Connect to Fetch and Update Locations

Now we’re ready for the fun part: fetching and updating locations.

Fetch Locations

Use the following PHP script to fetch your locations:

<?php
require 'vendor/autoload.php';

use Google\Client;
use GuzzleHttp\Client as HttpClient;

$client = new Client();
$client->setAuthConfig('path/to/your/service-account-file.json');
$client->addScope('https://www.googleapis.com/auth/business.manage');

$accessToken = $client->fetchAccessTokenWithAssertion()['access_token'];

$accountId = 'your-account-id'; // Replace with your actual account ID

$httpClient = new HttpClient();
$response = $httpClient->request('GET', "https://mybusiness.googleapis.com/v4/accounts/$accountId/locations", [
    'headers' => [
        'Authorization' => 'Bearer ' . $accessToken,
    ],
]);

$locations = json_decode($response->getBody()->getContents(), true);
print_r($locations);

Update Locations

To update a location, use:

<?php
require 'vendor/autoload.php';

use Google\Client;
use GuzzleHttp\Client as HttpClient;

$client = new Client();
$client->setAuthConfig('path/to/your/service-account-file.json');
$client->addScope('https://www.googleapis.com/auth/business.manage');

$accessToken = $client->fetchAccessTokenWithAssertion()['access_token'];

$accountId = 'your-account-id'; // Replace with your actual account ID
$locationId = 'your-location-id'; // Replace with your actual location ID

$httpClient = new HttpClient();
$response = $httpClient->request('PATCH', "https://mybusiness.googleapis.com/v4/accounts/$accountId/locations/$locationId", [
    'headers' => [
        'Authorization' => 'Bearer ' . $accessToken,
        'Content-Type' => 'application/json',
    ],
    'json' => [
        'locationName' => 'New Location Name',
        'address' => [
            'addressLines' => ['123 New Street'],
            'locality' => 'Stockholm',
            'administrativeArea' => 'SE',
            'postalCode' => '12345',
        ],
    ],
]);

echo $response->getStatusCode();

And there you have it! A complete guide to connecting a service account in Google Cloud with Google My Business, complete with API examples using the official Google PHP SDK library. Good luck!

Note: While this guide uses the Google PHP SDK, there are official libraries for other programming languages as well, such as Python, Java, and Node.js. You can find these libraries and their documentation on the Google API Client Libraries page.