I want to obtain a new "access token" based on the "refresh token" saved in database.

Here is the code I wrote:

GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder()
        .setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
        .setClientSecrets(CLIENT_ID, CLIENT_SECRET);
credentialBuilder.addRefreshListener(new MyCredentialRefreshListener());

credential = credentialBuilder.build();
credential.setRefreshToken("saved_refresh_token_from_database");

try {
    credential.refreshToken();
} catch (IOException e) {
    e.printStackTrace();
}


class MyCredentialRefreshListener implements CredentialRefreshListener {
    public void onTokenResponse(Credential cr, TokenResponse tr) {
       System.out.println("Credential was refreshed successfully.");
    }

    public void onTokenErrorResponse(Credential cr, TokenErrorResponse tr) {
        System.out.println(tr);
    }
 }

I get this message:

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad

Request { "error" : "invalid_grant" }

I use the same CLIENT_ID, CLIENT_SECRET and "refresh token" in a php script and there I managed to get the new "access token" using "refresh token" when "access token" expired.

I wrote the code based on javadoc.google-oauth-java-client.

Any person here knows how to modify the code to obtain the new access token ?

Thanks in advance.

share|improve this question
feedback

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.