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

时间:2013-08-19 08:34:40

标签: java swing oauth-2.0 google-drive-api google-docs-api

以前,为了使用Google云端硬盘服务,我们在桌面应用程序中使用以下简单的用户名和密码登录Google文档列表API

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

与谷歌公告https://developers.google.com/google-apps/documents-list/一样,Google文档列表API的第3版已于2012年9月14日正式弃用。它将继续按照我们的弃用政策运作,但我们建议您继续使用到Google云端硬盘API。

因此,在我们的桌面Swing应用程序中,我们需要重写代码以获得对Google Drive服务的身份验证。

通过引用在控制台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. 用户需要手动打开浏览器。
  2. 输入用户名和密码。
  3. 然后,他将从浏览器中获取一个特殊字符串,他需要将其复制并粘贴到控制台。
  4. enter image description here

    我认为通过使用网络视图组件Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application,我可以非常简单地完成此过程,以便上述任务自动完成。

    但是,当我关闭桌面Swing应用程序时,webview的组件以前的历史会话将被销毁。用户每次启动桌面应用程序时都需要输入密码和用户名。

    enter image description here

    从适用于Windows的官方Google云端硬盘应用中,我发现它确实能够记住用户的上次身份验证。用户只需输入一次用户名和密码即可。然后,即使重新启动计算机,他也可以反复使用Google云端硬盘服务。

    我可以知道如何实现类似的行为吗?

1 个答案:

答案 0 :(得分:2)

您需要永久存储网页视图的Cookie,并在再次启动网络视图后再次设置它们。

相关问题