472,121 Members | 1,567 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

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

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

Expand|Select|Wrap|Line Numbers
  1. GoogleCredential.Builder credentialBuilder = new GoogleCredential.Builder()
  2.         .setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
  3.         .setClientSecrets(CLIENT_ID, CLIENT_SECRET);
  4. credentialBuilder.addRefreshListener(new MyCredentialRefreshListener());
  5.  
  6. credential = credentialBuilder.build();
  7. credential.setRefreshToken("saved_refresh_token_from_database");
  8.  
  9. try {
  10.     credential.refreshToken();
  11. } catch (IOException e) {
  12.     e.printStackTrace();
  13. }
  14.  
  15.  
  16. class MyCredentialRefreshListener implements CredentialRefreshListener {
  17.     public void onTokenResponse(Credential cr, TokenResponse tr) {
  18.        System.out.println("Credential was refreshed successfully.");
  19.     }
  20.  
  21.     public void onTokenErrorResponse(Credential cr, TokenErrorResponse tr) {
  22.         System.out.println(tr);
  23.     }
  24.  }
I get this message:

Expand|Select|Wrap|Line Numbers
  1. com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad
  2.  
  3. 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.
Nov 12 '12 #1
0 2940

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by SBK | last post: by
10 posts views Thread by soup_or_power | last post: by
5 posts views Thread by Mazharrana | last post: by
2 posts views Thread by =?Utf-8?B?Y2FzaGRlc2ttYWM=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.