0

I am trying to get GoogleCredential for my project like given below,

GoogleCredential credential = new GoogleCredential.Builder()
                     .setTransport(httpTransport)
                     .setJsonFactory(jsonFactory)
                     .setServiceAccountId("xxx@famous-store-115417.iam.gserviceaccount.com")
                     .setServiceAccountScopes(SCOPES)
                     .setServiceAccountPrivateKeyFromP12File(new File(p12))
                     .build();

but I am getting below error,

Exception in thread "main" com.google.gdata.util.AuthenticationException: Failed to refresh access token: 400 Bad Request
{
  "error" : "invalid_grant"
}
    at com.google.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:260)
    at com.google.gdata.client.GoogleAuthTokenFactory.handleSessionExpiredException(GoogleAuthTokenFactory.java:702)
    at com.google.gdata.client.GoogleService.handleSessionExpiredException(GoogleService.java:738)
    at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:649)
    at com.google.gdata.client.Service.getFeed(Service.java:1017)
    at ImportAwards.openFeed(ImportAwards.java:49)
    at ImportAwards.importAwards(ImportAwards.java:62)
    at ImportAwards.main(ImportAwards.java:29)
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.gdata.client.GoogleAuthTokenFactory$OAuth2Token.refreshToken(GoogleAuthTokenFactory.java:258)
    ... 7 more

I know it's permission related error, but I am using right key and service account id, and I have also searched it through web but it is the same solution suggested. I have refereed this as well.

Any suggestions are appreciated, thanks in advance. What should I am missing?

Community
  • 1
  • 1
Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82

1 Answers1

0

Finally I found the solution is,

I was using Goodle Drive API, so I need to share that document with service account (xxx@famous-store-115417.iam.gserviceaccount.com) as well. And after that it was working properly.

I hope it helps to someone else.

Regards, Vishal Z.

Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
  • Did you not have to setServiceAccountUser ?? it would still work without it? – Sardonic Jul 06 '16 at 21:56
  • my code throws "Not Authorized to access this resource/api" error without `.setServiceAccountUser("admin@domain.com")` and without it, it would throw invalid_grant error.... – Sardonic Jul 06 '16 at 22:03