(JAVA) Google API - unable to obtain new “access token” using “refresh token

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xoip
    New Member
    • Nov 2012
    • 1

    (JAVA) Google API - unable to obtain new “access token” using “refresh token

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

    Here is the code I wrote:

    Code:
    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:

    Code:
    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.
Working...