Google Analytics : Get access token from refresh token

1.9k views Asked by At

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.

1

There are 1 answers

1
mhgbrown On

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.