Fetching access token from refresh token using Jav

2019-01-18 03:44发布

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?

4条回答
劫难
2楼-- · 2019-01-18 04:16

Follow this example:

private static void getAccessTokenFromRefreshToken() throws IOException {
    GoogleCredential credentials = new GoogleCredential.Builder()
        .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
        .setJsonFactory(JSON_FACTORY).setTransport(httpTransport).build()
        .setRefreshToken(REFRESH_TOKEN);

    String accessToken = credentials.getAccessToken();
    System.out.println("Access token before: " + accessToken);

    credentials.refreshToken();

    accessToken = credentials.getAccessToken();
    System.out.println("Access token after: " + accessToken);
}

Output:

Access token before: null
Access token after: ya29.u4HC22-Avc0aaDC0g0zj1jhz2yjsJrm8qm0hU2eVeBrf6DKj3CcHDQ42KARH4y_d364-b
查看更多
\"骚年 ilove
3楼-- · 2019-01-18 04:17

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.

查看更多
Animai°情兽
4楼-- · 2019-01-18 04:25

Try this code. This is a mix solution from Alain + https://cloud.google.com/bigquery/authorization. this worked for me.

GoogleCredential credential = createCredentialWithRefreshToken(
        HTTP_TRANSPORT, JSON_FACTORY, new TokenResponse().setRefreshToken(refreshToken));

credential.refreshToken(); 

String newAccessToken = credential.getAccessToken();

public static GoogleCredential createCredentialWithRefreshToken(HttpTransport transport, 
        JsonFactory jsonFactory, TokenResponse tokenResponse) {
    return new GoogleCredential.Builder().setTransport(transport)
        .setJsonFactory(jsonFactory)
        .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
        .build()
        .setFromTokenResponse(tokenResponse);
}
查看更多
Evening l夕情丶
5楼-- · 2019-01-18 04:36

Just to explain my experience.

Had the same problem (access token to null) the problem was that I had been tested without sending setAccessType ("offline") and from the account access was allowed access.

Then I put on my code setAccessType ("offline") code but still refresh token to null.

My solution was to revoke permission from the account I want to access (https://accounts.google.com/b/0/IssuedAuthSubTokens?hl=en). In the next test I grant it.

查看更多
登录 后发表回答