使用Java从刷新令牌获取访问令牌 [英] Fetching access token from refresh token using Java

查看:1164
本文介绍了使用Java从刷新令牌获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此处给出的示例程序从授权代码获取了刷新令牌和访问令牌 https ://developers.google.com/drive/credentials#retrieve_oauth_20_credentials



但是没有示例程序从刷新令牌获取访问令牌,即当我们没有授权码。任何指针?有没有什么办法可以使用刷新令牌和访问令牌来实例化驱动器服务对象?

DrEdit Java 示例提供了如何从Google App Engine数据存储检索存储的凭证的示例。



如果使用其他类型的凭证存储,则可以使用以下代码使用存储的凭证实例化新的OAuth 2.0凭证:

<$ p $ b $ set $ b $ set $($)$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ (< REFRESH_TOKEN>)。

编辑:更正了代码中的错字。


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?

解决方案

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.

这篇关于使用Java从刷新令牌获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆