My Business
1.7K members online now
1.7K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

How to authenticate and get or refresh access token using java client library for GMB API

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi,

we want to use java client library for GMB api v3.2 to create location extensions and map to adwords accounts. we want have a single GMB account mapped to multiple adwords accounts by linking these adwords accounts in GMB account.

 

1)Is above approach feasible? Can we link multiple adwords accounts to same GMB account and regenerate refresh token for all adwords accounts using both GMB and adwords scope for authentication.Each adwords accounts have different client id and client secret. 

 

2)In case we want to have separate authentication for GMB from adwords, whats the best way to do it for installed app?? In sample examples, I see GoogleClientSecrets.load() being used but it takes input stream as param which has only client Id and client secret. we save client id and client secret as String so we can't pass it as inputStream? what about the refresh token?? how do we set refresh token for GMB Oauth.

 

Is there any API or method in gmb similar to adwords where we use OfflineCredentials.Builder() as below to set client id,client secret and refresh token or can we use the same for GMB as well?? but I din't find the option of setting OfflineCredentials.Builder().forApi(Api.GMB)

 

new OfflineCredentials.Builder().forApi(Api.ADWORDS).withHttpTransport(theHttpTransport)
.withClientSecrets(aClientId, aClientSecret).withRefreshToken(aRefreshToken).build()
.generateCredential();

 

sample code using input stream to load client id and client secret:

// 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/plus.business.manage"))
     
.setDataStoreFactory(dataStoreFactory).build();
 
// Returns the credential.
 
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

 

Thanks,

Alok

1 Expert replyverified_user

How to authenticate and get or refresh access token using java client library for GMB API

Visitor ✭ ✭ ✭
# 2
Visitor ✭ ✭ ✭

Hi,

Any update on above questions

Re: How to authenticate and get or refresh access token using java client library for GMB API

Google Employee
# 3
Google Employee

Hi @alok s,

 

1) Yes, if you want to have one Google My Business account, please follow best practice for creating locations in Google My Business and linking your Google My Business account to AdWords. Yes, you can regenerate refresh token for all adwords accounts using the Google My Business.

 

2) For generating separate OAuth 2.0 access tokens for each Google Account, when you use your whitelisted project’s credentials to obtain an OAuth 2.0 access token for authentication via the Google My Business API, you should be logging in with your specific Google Account. When this token is provided and you are making API calls, you will be able to view and manage all the existing locations within that specific account. You should repeat this process with a single set of OAuth 2.0 credentials for your separate Google Accounts to obtain a token for each Google Account.

 

In addition, when using the OAuth 2.0 client ID for authorization, you should store the refresh token for future use and use the access token to access the API. Once the access token expires, the application should use the refresh token to obtain a new one. This way, your application will always be able to request a new access token when necessary. This process requires a user to manually authorize the application only once.

 

No, the Google My Business API doesn’t have the same method OfflineCredentials.Builder() as adwords. But you can use setAccessType("offline") method for this purpose in the Google My Business API.

 

You can use the following code snippet for persisting the refresh token with the Java Client Library:

 

  // set up authorization code flow
  GoogleAuthorizationCodeFlow flow =
      new GoogleAuthorizationCodeFlow.Builder(
              httpTransport,
              JSON_FACTORY,
              clientSecrets,
              Collections.singleton("https://www.googleapis.com/auth/plus.business.manage"))
          .setDataStoreFactory(dataStoreFactory)
          .setAccessType("offline")
          .setApprovalPrompt("force")
          .build();
  // authorize
  return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");

 

Thanks,

The Google My Business API team