Google My Business: How to generate access token without user interaction

1.4k views Asked by At

I want to use a Google My Business API to search and manage the businesses of our clients using my organization account detail.

I want to use API as an Agency as I want to manage our clients' businesses.

I've submitted the API access form and got confirmation to use the APIs. Also, I've generated the OAuth credentials and service-account in the same account.

But as per the GMB API document, we need to pass an access token in order to access an API.

I've reviewed the authentication process using OAuth and service accounts. As per my requirement, I want to generate access token at the back end side directly from my NodeJS server code without user interaction.

I want to use my account detail to use APIs so need to get permission from the user as I do not need any user information.

I've tried the following for use as a service-account but it returns an error.

const keys = require("./config/gmb.json");
const {OAuth2Client,GoogleAuth,JWT} = require('google-auth-library');
const client = new JWT({
   email: keys.client_email,
   key: keys.private_key,
   scopes: ['https://www.googleapis.com/auth/business.manage'],
});
console.log(client.credentials);  // It returns refresh_token=jwt-placeholder, expiry_date=1
const tokenInfo = await client.getTokenInfo(client.credentials.access_token); // It returns Error: invalid_token 

So anyone has an idea on this. Your help will appreciatable.

1

There are 1 answers

0
Keyur Chavda-kc1994 On BEST ANSWER

I got the answer to my question.

If we want to get the access token of any user account first we need to prompt the user for OAuth and once the user gives its account permission we need to store the user account details with refresh-token that google will give once we complete the user OAuth process.

Now, whenever we need the access token of any user account we can get it using the refresh token of user account details that have stored after completing the OAuth process.

Note: The refresh token can expire if the user changes his account password or some details which affect user privacy.

I've done this OAuth process and getting the access token of a user in NodeJs.