1
INFO: com.google.api.client.auth.oauth2.TokenResponseException: 400 OK
{
  "error" : "invalid_grant"
}

my code is

GoogleCredential credential = new GoogleCredential.Builder()
            .setTransport(httpTransport)
            .setJsonFactory(js)
            .setServiceAccountId(emailId)
            .setServiceAccountScopes(scopes)
            .setServiceAccountPrivateKeyFromP12File(new File(privateKeyPath)).build();

    System.out.println("SERVICE ACCOUNT SERVLET CALLED");

    credential.refreshToken();

this method is running fine but suddenly it starts to give error.

pes502
  • 1,597
  • 3
  • 17
  • 32
Ajay
  • 13
  • 1
  • 5

1 Answers1

4

Possible reasons:

1) I was having a similar problem caused by the time on my server being incorrect. Make sure your system clock is synchronized.

2) You should reuse the access token you get after the first successful authentication. You will get an invalid_grant error if your previous token has not expired yet. Cache it somewhere so you can reuse it.

3) Make sure that you pass the access token, and not refresh token as that fails. When you run, it turns the OAUth2 refresh token into an access token and passes that to the service. If you pass the raw refresh token, this is not acceptable for API access without turning it into a short-lived access token.

4) You might have reached the refresh token limit. Certain Google APIs have this, some explanation you can find here: https://developers.google.com/compute/docs/authentication

5) You also can revoke it. Go to your Google API Console ( https://code.google.com/apis/console/ ) and revoke your Client Secret under Client ID for installed applications. Be sure to also update your code with the new Client Secret

Pentium10
  • 204,586
  • 122
  • 423
  • 502