如何通过我自己开发的用于Web应用程序的OAUTH 2框架使用Google Drive Client Library for Java? [英] How to use Google Drive Client Library for Java with my homegrown OAUTH 2 Framework for web application?
问题描述
我的web应用程序已经使用REST和/或signpost-oauth库成功地与Facebook,LinkedIn,Google等进行了OAUTh身份验证。
因此,一旦我已经使用我的网络应用从GoogleAPI服务器获得了ACCES_TOKEN,我想使用Google Drive Client访问文件等。
这是谷歌云端硬盘示例,但它依赖于使用Google授权代码流(我无法很好地理解,以便与我的Java EE Web应用程序一起使用)
//授权
凭证凭证=授权();
//设置全局Drive实例
drive = new Drive.Builder(HTTP_TRANSPORT,JSON_FACTORY,凭证)
.setApplicationName(APPLICATION_NAME).build();
如何更改此设置以使用ACCESS_TOKEN和我已通过Web应用程序的OAUTH获取的其他凭据如果你已经有一个访问令牌,你可以简单地这样做:
$ b $ b
凭证凭证= new GoogleCredential()。setAccessToken(yourAccessToken);
如果您有一个Access令牌和一个刷新令牌, 更好,因为您可以从客户端库中获取自动令牌刷新,您应该这样做:
凭证凭证=新的GoogleCredential.Builder( )
.setClientSecrets(CLIENT_ID,CLIENT_SECRET)
.setJsonFactory(JSON_FACTORY)
.setTransport(HTTP_TRANSPORT)
.build()
.setAccessToken(yourAccessToken)
.setRefreshToken(yourRefreshToken);
另请检查 GoogleCredential Javadoc 以获取更多示例。
My web application already does OAUTh authentication successfully with Facebook, LinkedIn, Google etc. using REST and/or signpost-oauth library.
So once I already have ACCES_TOKEN from GoogleAPIs server using my web app , I want to use Google Drive Client to access files etc.
This is the google Drive example but it relies upon using Google authorization code flow ( which I can't understand well enough to use with my Java EE web app )
// authorization
Credential credential = authorize();
// set up the global Drive instance
drive = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME).build();
How do I change this to use ACCESS_TOKEN and other credentials that I have already obtained through my web application's OAUTH framework ?
If you already have an Access Token, you can simply do this:
Credential credential = new GoogleCredential().setAccessToken(yourAccessToken);
If you have an Access Token AND a Refresh Token, which is much better since you can get automatic token refresh from the client library, you should do this:
Credential credential = new GoogleCredential.Builder()
.setClientSecrets(CLIENT_ID, CLIENT_SECRET)
.setJsonFactory(JSON_FACTORY)
.setTransport(HTTP_TRANSPORT)
.build()
.setAccessToken(yourAccessToken)
.setRefreshToken(yourRefreshToken);
Also Check the GoogleCredential Javadoc for more examples.
这篇关于如何通过我自己开发的用于Web应用程序的OAUTH 2框架使用Google Drive Client Library for Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!