Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching access token from refresh token using Java

I got the refresh token and access token from the authorization code by using the sample program given here https://developers.google.com/drive/credentials#retrieve_oauth_20_credentials

But there is no sample program to get the access token from refresh token i.e., when we dont have authorization code. Any pointers? Is there any way to Instantiate a drive service object using only refresh token and access token?

like image 881
VijayRaj Avatar asked May 10 '12 11:05

VijayRaj


People also ask

How can I get access token and refresh token?

To get an access token using a refresh token, you must first get the refresh token. Then you use the refresh token from then on to generate an access token.

How can I get authorization code from refresh token?

To get a new access token, use the refresh token as you would an authorization code, but with a grant_type value of refresh_token and a refresh_token parameter that holds the contents of the refresh token. The type of grant being used. To exchange a refresh token for an access token, use refresh_token .


1 Answers

The DrEdit Java sample has an example on how to retrieve stored Credentials from the Google App Engine Datastore.

If using another type of credentials store, you can use the following code to instantiate new OAuth 2.0 Credentials using stored tokens:

GoogleCredential credentials = new GoogleCredential.Builder()
    .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
    .setJsonFactory(jsonFactory).setTransport(transport).build()
    .setRefreshToken("<REFRESH_TOKEN>").setAccessToken("<ACCESS_TOKEN>");

EDIT: Corrected a typo in the code.

like image 66
Alain Avatar answered Sep 22 '22 09:09

Alain