【问题标题】:Prevent oAuth 2.0 Google Drive Credentials from expiring防止 oAuth 2.0 Google Drive Credentials 过期
【发布时间】:2014-05-03 19:03:23
【问题描述】:

目前,我正在将使用 oAuth 2.0 的 Google Drive API 与我的桌面应用程序集成。

我意识到,一段时间后,存储在磁盘上的Credential 将过期。发生这种情况时,我需要再次通过浏览器提示用户,要求他们输入用户名和密码。

https://github.com/yccheok/jstock/blob/master/src/org/yccheok/jstock/google/MyAuthorizationCodeInstalledApp.java#L53

public Pair<Pair<Credential, Userinfoplus>, Boolean> authorize(String userId) throws IOException {
    try {
        Credential credential = flow.loadCredential(userId);
        if (credential != null
            && (credential.getRefreshToken() != null || credential.getExpiresInSeconds() > 60)) {
            Userinfoplus userinfoplus = org.yccheok.jstock.google.Utils.getUserInfo(credential);
            return new Pair<Pair<Credential, Userinfoplus>, Boolean>(new Pair<Credential, Userinfoplus>(credential, userinfoplus), true);
        }
        // open in browser
        redirectUri = receiver.getRedirectUri();

由于用户将继续使用我的桌面应用程序数天、数周和数月,他们希望只提供一次用户名和密码。

有什么方法可以防止Credential 过期吗?我相信这是可以实现的,因为桌面版 Google 云端硬盘从不提示我输入用户名和密码。

【问题讨论】:

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


    【解决方案1】:

    我们可以通过调用refreshToken“刷新”Credential

    boolean success = false;
    if (credential.getExpiresInSeconds() <= 60) {
        if (credential.refreshToken()) {
            success = true;
        }
    } else {
        success = true;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 1970-01-01
      • 2022-01-11
      • 2019-03-22
      • 2012-09-19
      • 1970-01-01
      相关资源
      最近更新 更多