4

I am working with google analytics api in Java. I have a code which fetches access token from refresh token. We store the refresh token in the database and while requesting, retrieve access token.

After password change, our request token became invalid. So, I generated a new refreshtoken by browsing the url (https://developers.google.com/oauthplayground) and following the instructions. I got the new refresh token. Now, when I am executing the code to retrieve access token, it throws null pointer exception. Following is the code:

RefreshTokenRequest request = new GoogleRefreshTokenRequest(HTTP_TRANSPORT, JSON_FACTORY, refreshToken,
                    this.clientId, this.clientSecret);
            TokenResponse response = request.execute(); 

When execute method is invoked, it throws the following exception:

java.lang.NullPointerException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:191)
    at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)
    at com.google.api.client.json.jackson2.JacksonFactory.createJsonParser(JacksonFactory.java:96)
    at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:85)
    at com.google.api.client.json.JsonObjectParser.parseAndClose(JsonObjectParser.java:81)
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:88)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
    at com.google.api.client.googleapis.auth.oauth2.GoogleRefreshTokenRequest.execute(GoogleRefreshTokenRequest.java:125)
    at com.google.api.client.googleapis.auth.oauth2.GoogleRefreshTokenRequest.execute(GoogleRefreshTokenRequest.java:75)
    at GARTest.main(GARTest.java:22)

Can anyone please point where am I doing wrong? I am able to get the access token from google oAuth UI.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
user3968762
  • 439
  • 1
  • 5
  • 11
  • I'm afraid it is an open issue for the GoogleApi java library: https://github.com/google/google-api-java-client/issues/932 please, share if you have any solution. – Artem Mar 12 '15 at 03:39
  • You should not be using Oauthplayground to get a RefreshToken, this should be part of your code. Also password change will not effect an Oauth2 RefreshToken that is the whole point of OAuth. – Linda Lawton - DaImTo Mar 12 '15 at 14:52

1 Answers1

2

This may have your answer: https://github.com/googleads/googleads-java-lib/issues/19

Github user jyothi462 says that his NPE was ultimately caused by using the wrong client id and secret. This was also the problem in my case.

That issue was resolved with an update the error message, making it more descriptive.

mhgbrown
  • 91
  • 6
  • The stack trace you've reference is very different from the one on the description. The closer one is: https://github.com/google/google-api-java-client/issues/932 – Artem Mar 12 '15 at 03:40