简体   繁体   English

记住桌面应用程序中Google Drive API的最后一次oAuth2会话

[英]Remember the last oAuth2 session for Google Drive API in Desktop application

Previously, in order to use Google Drive service, we are using the following simple username and password login in Desktop application with Google Documents List API 以前,为了使用Google云端硬盘服务,我们在带有Google Documents List API的桌面应用程序中使用以下简单的用户名和密码登录

DocsService client = new DocsService(getCloudApplicationName());
client.setUserCredentials(username, password);

As in Google announcement, https://developers.google.com/google-apps/documents-list/ , Version 3 of the Google Documents List API has been officially deprecated as of September 14, 2012. It will continue to work as per our deprecation policy, but we encourage you to move to the Google Drive API. 与Google公告一样,自2012年9月14日起,谷歌文档列表API的版本3已正式弃用https://developers.google.com/google-apps/documents-list/ 。我们的弃用政策,但我们建议您转到Google Drive API。

So, in our desktop Swing application, we need to rewrite the code to gain authentication to Google Drive service. 因此,在我们的桌面Swing应用程序中,我们需要重写代码以对Google云端硬盘服务进行身份验证。

By referring to code example run at console https://developers.google.com/drive/quickstart-java#step_3_set_up_the_sample 通过参考在控制台https://developers.google.com/drive/quickstart-java#step_3_set_up_the_sample上运行的代码示例

Please open the following URL in your browser then type the authorization code:
  https://accounts.google.com/o/oauth2/auth?access_type=online&client_id=888888888888.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/drive
  1. User need to open up the browser manually. 用户需要手动打开浏览器。
  2. Key in his username and password. 输入他的用户名和密码。
  3. He will then obtain a special string from browser, where he needs to copy and paste to the console. 然后,他将从浏览器中获取一个特殊的字符串,在该字符串中,他需要将其复制并粘贴到控制台。

在此处输入图片说明

I think I can pretty much simply the process, by using a web view component Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application , so that the above task will be automated. 我想我可以通过使用将JavaFX 2.0 WebView集成到Swing Java SE 6 Application中的Web视图组件来简化整个过程,从而使上述任务自动化。

However, the webview's component previous history session will be destroyed as I close my desktop Swing application. 但是,当我关闭桌面Swing应用程序时,Webview的组件以前的历史记录会话将被破坏。 User need to key in his password and username every-time he launch the desktop app. 用户每次启动桌面应用程序时都需要输入密码和用户名。

在此处输入图片说明

From official Google Drive app for Windows, I realize it does have the capability to remember the last authentication of the user. 从Windows的官方Google Drive应用程序中,我意识到它确实具有记住用户的最新身份验证的功能。 User just need to key in his username and password once. 用户只需要输入一次用户名和密码即可。 He can then use the Google Drive service again and again, even after restarting the computer. 然后,即使重新启动计算机,他也可以一次又一次使用Google云端硬盘服务。

May I know how I can achieve the similar behavior? 我可以知道如何实现类似的行为吗?

您需要永久存储Web视图的Cookie,并在再次启动Web视图时再次设置它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM