Search code examples
node.jsoauth-2.0google-apigoogle-oauthgoogle-business

Get Google business reviews server side


I'm trying to get a list of reviews of my Google business through the API to display them on my website. But I can't figure out how to authenticate the API server side. The documentation only mentions OAuth2.0 authentication from the client side with redirect URLs, but there won't be a client going to a confirmation page in this case.

I just want to be able to perform this request in Node.js:

GET https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews

I've already submitted the application and been approved for a Business API and enabled the various APIs in my account. I have created OAuth2.0 credentials. I'm just not sure how to move forward from here.

How can I authenticate Google Business API requests on the server?


Solution

  • I ended up putting together an answer through lots of searching. Google documentation is all over the place.

    A basic example of getting reviews is below. But to get it to work there are a few steps first.

    1. You'll need to add a Service Account in your API credentials page
      • The name, ID, and description aren't particularly important. Just something that makes sense to you
    2. Go to the service account details -> keys -> add key -> create new key -> JSON
      • This will download a key file to your computer. Keep it private.
    3. Grant domain wide delegation for your service account
      • To do this, you'll need to be an admin of the account if you're part of an organisation
      • It will ask for a Client ID, but it is called Unique ID in the service account details. They're the same thing.
      • Add whatever scopes you need for the services you want to access. For reviews, the scope listed in the example below is enough.
    4. The subject field in google.auth.JWT needs to be an admin of the account. I used my own email.

    That should be it! You should now be able to fill out the values in the example below and access the API from a server. Other services may require different scopes.

    You can get account and location info from the API docs. The endpoints and data formats are fairly well documented. Just authentication isn't very well explained it seems.

    import axios from 'axios';
    import {google} from 'googleapis';
    
    import key from './key.json' assert {type: 'json'};
    
    main();
    
    async function main(){
        const reviews=await getReviews();
    }
    
    async function getReviews(){
        const token=await authenticate();
        const accountId='YOUR ACCOUNT ID';
        const locationId='YOUR LOCATION ID';
        const url=`https://mybusiness.googleapis.com/v4/accounts/`+
              `${accountId}/locations/${locationId}/reviews`;
        const resp=await axios.get(url, {
            headers: {
                authorization: `Bearer ${token}`
            }
        });
        return resp.data.reviews;
    }
    
    async function authenticate(){
        const scopes=[
            'https://www.googleapis.com/auth/business.manage'
        ];
    
        const jwt=new google.auth.JWT({
            email: key.client_email,
            key: key.private_key,
            subject: 'ADMIN EMAIL',
            scopes
        });
    
        const resp=await jwt.authorize();
        return resp.access_token.replace(/\.{2,}/g, '');
    }