How to get an access token using a refresh token in Java?
Asked Answered
F

2

5

I am currently implementing Contact Application using Google Contact API in Java. I have completed steps of authorization and obtained an access token and a refresh token.

Now I have the CLIENT_ID , CLIENT_SECRET AND REFRESH_TOKEN with me . But the access token is getting expired within an hour.

Can someone tell how to automatically generate an access token using a refresh token in Java?

Fisherman answered 18/6, 2018 at 6:33 Comment(0)
P
7

You can use Google OAuth2 client library for getting a new access token using a refresh token.

Here is my code for getting a new access 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;
}

For more information read the official Google API guide:

Pricilla answered 27/1, 2020 at 6:32 Comment(0)
E
1

I have implemented this scenario in two ways. Not sure they are the best or not but works well for me.

In both cases you have to store the refresh token along with the email id of the user. Then you have to make a HTTP request in the below format.

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded

client_id=**<your_client_id>**&
client_secret=**<your_client_secret>**&
refresh_token=**<refresh_token>**&
grant_type=refresh_token

It will return the access_token and expires_in Source

Now the question is how and when to do the http request. So for that I have two ways.

1st one

Store the emailid,refresh token, access token and the current time+3600 seconds. In your database you can schedule to check the expire time in every 5 min and if current time of any user is going to reach(before 5 or 10 min) the expire time, get the refresh token and update the value for that particular user. And during api call just fetch the access token of the user.

2nd one

When user do login in your site, get the access token and current time+ 3600secs and store it in browser cookies. Now before doing any api call just check whether the current time(time when api call is done) is less than the expire time(stored in cookie). If its true then you can use the previous access token else get a new one and again update the cookie. Also you have to put another condition that if the cookie is not present at all then also you have to get new refresh token.

Exuberate answered 18/6, 2018 at 7:11 Comment(3)
Thanks for your response . But I can't understand your HTTP request format. Can you explain how to use client id , client secret and refresh token in HTTP request to generate new access token.Fisherman
you have to make a HTTP POST request to googleapis.com/oauth2/v4/token url with following request body client_id: <YOUR_CLIENT_ID>, client_secret: <YOUR_CLIENT_SECRET>, refresh_token: <REFRESH_TOKEN_FOR_THE_USER>, grant_type: refresh_tokenExuberate
Now i'm getting an exception : Exception in thread "main" java.io.IOException: Server returned HTTP response code: 401 for URL: googleapis.com/oauth2/v4/token. Here is my code #50907605 . Tell me where i'm wrong.Fisherman

© 2022 - 2024 — McMap. All rights reserved.