0

I am trying to get data of user of Google AdSense using OAuth2 and I have followed all the Documentation which is here.

And I successfully get all response which is expected but the problem is that the refresh token is expired within access token as access token expires after 1 hour and I'm unable to make another access token request from refresh token because it also expires.

In one line, Refreh Token expires after 1 hour and user needs to login again. That's main problem.

The Code I have tried to get access token from refresh token:

public TokenResponse refreshAccessToken(String refreshToken) throws IOException {
    TokenResponse response = new GoogleRefreshTokenRequest(
            new NetHttpTransport(),
            new JacksonFactory(),
            refreshToken, 
            "your clientId",
            "your clientSecret")
            .execute();
    System.out.println("Access token: " + response.getAccessToken());

    return response;
}

Here's is app activity code.

I have tried many searches but they didn't help. If there is any other method to get the access token, please help me to reach out there.

Thanks In Advance.

Any other working way would be Gold for me.

=========================== UPDATE ========================

I have got my mistake in my project.

Adarsh Raj
  • 325
  • 4
  • 17

1 Answers1

0

I think you get a new refresh token after refreshing the access token. You need to update your refresh token as it expires when it is used to refresh an access token.

In your provided example you only extract access token and ignore the rest of the response.

https://cloud.google.com/apigee/docs/api-platform/security/oauth/access-tokens

multicatch
  • 192
  • 4
  • Thanks You. but I can't refresh access token because refresh tokens expires just when access token expires. – Adarsh Raj May 12 '21 at 07:53
  • Did you know any another way? – Adarsh Raj May 12 '21 at 07:58
  • You should refresh the access token a few seconds before it expires then – multicatch May 12 '21 at 08:00
  • I have done this one also but the result is when first access token expires then new one also expires. – Adarsh Raj May 12 '21 at 08:05
  • When you refesh an access token then the first one is no longer valid and you should use the new one. Repeat this operation as long as you need to access the API as every token expires. Refreshing a token does not cause the existing token to live longer. You can also fetch a new access token instead of refreshing it if refreshing fails for some reason. – multicatch May 12 '21 at 08:34
  • can you provide any guide for getting new access token as it expires... – Adarsh Raj May 12 '21 at 08:40