How to use Google Drive Client Library for Java with my homegrown OAUTH 2 Framework for web application?

荒凉一梦 提交于 2020-01-24 19:28:24

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/15986911/how-to-use-google-drive-client-library-for-java-with-my-homegrown-oauth-2-framew

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!