TokenResponseException while connectiong to Google adwords API using service account

210 views
Skip to first unread message

edduenas...@gmail.com

unread,
Nov 27, 2014, 6:48:35 AM11/27/14
to adwor...@googlegroups.com

We tried to create a code to obtain Google Adwords API connection using service account but we are getting the following errors.

 

 

Caused by: com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request

{

  "error" : "invalid_grant"

}

       at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)

       at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)

       at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)

       at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)

       at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)

       at com.google.api.ads.common.lib.auth.OAuth2Helper.callRefreshToken(OAuth2Helper.java:70)

       at com.google.api.ads.common.lib.auth.OAuth2Helper.refreshCredential(OAuth2Helper.java:56)

       at com.google.api.ads.common.lib.auth.AuthorizationHeaderProvider.getOAuth2Header(AuthorizationHeaderProvider.java:102)

 

 

 

The code for google adwords API connection using service account is :

 

 

 

GoogleCredential serviceAccountCredential = new GoogleCredential.Builder()

.setTransport(new NetHttpTransport.Builder().setProxy(proxy).build())

.setJsonFactory(new JacksonFactory())

.setServiceAccountId("****.apps.googleusercontent.com")

.setServiceAccountScopes(Collections.singleton("https://adwords.google.com/api/adwords"))

.setServiceAccountPrivateKeyFromP12File(keyFile)

.setServiceAccountUser("****@gmail.com")

.build();

return serviceAccountCredential;

 

AdWordsSession adSession = new AdWordsSession.Builder()

.fromFile()

.withOAuth2Credential(credential)

.build();

 

 

Danial Klimkin

unread,
Nov 28, 2014, 7:20:14 AM11/28/14
to adwor...@googlegroups.com
Hello,


First of all, is there any strong reason to use service account? It is only available for Google App domains, do you use one?


-Danial, AdWords API Team.

edduenas...@gmail.com

unread,
Dec 12, 2014, 7:57:38 PM12/12/14
to adwor...@googlegroups.com
Thanks Danial,
we tried accessing OAuth for API through "Installed Application" credentials and it worked, but what is the suggested/best way to make calls from offline java batch jobs to adwords api? 
we figured that service accounts are best as they won't need any user interaction (server to server interactions).

We are not on Google Apps domain.

Danial Klimkin

unread,
Dec 15, 2014, 8:31:41 AM12/15/14
to adwor...@googlegroups.com
Hello,


You need to store the refresh token and use it for each run. Please see the guide here:



-Danial, AdWords API Team.
Reply all
Reply to author
Forward
0 new messages