【问题标题】:Remember the last oAuth2 session for Google Drive API in Desktop application记住桌面应用程序中 Google Drive API 的最后一次 oAuth2 会话
【发布时间】:2013-08-20 23:57:37
【问题描述】:

以前,为了使用 Google Drive 服务,我们在桌面应用程序中使用以下简单的用户名和密码登录 Google Documents List API

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

如 Google 公告中所述,https://developers.google.com/google-apps/documents-list/,Google Documents List 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. 然后他将从浏览器中获取一个特殊的字符串,他需要将其复制并粘贴到控制台。

我想我可以通过使用 Web 视图组件 Integrating JavaFX 2.0 WebView into a Swing Java SE 6 Application 来简化该过程,从而自动完成上述任务。

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

从适用于 Windows 的官方 Google Drive 应用程序中,我意识到它确实能够记住用户的最后一次身份验证。用户只需要输入他的用户名和密码一次。然后,他可以一次又一次地使用 Google Drive 服务,即使在重新启动计算机之后也是如此。

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 好的。感谢您的提示。将研究如何做到这一点的更多细节。您是否遇到过任何类似的代码示例?
    猜你喜欢
    • 2022-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多