Java 防止oAuth 2.0 Google驱动器凭据过期

Java 防止oAuth 2.0 Google驱动器凭据过期,java,oauth-2.0,google-drive-api,Java,Oauth 2.0,Google Drive Api,目前,我正在将使用oAuth 2.0的Google Drive API与我的桌面应用程序集成 我意识到,在一段时间后,存储在磁盘上的凭证将过期。发生这种情况时,我需要再次通过浏览器提示用户输入用户名和密码 公共对授权(字符串userId)引发IOException{ 试一试{ 凭证凭证=flow.loadCredential(userId); if(凭证!=null &&(credential.getRefreshToken()!=null | | credential.getExpiresI

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

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

公共对授权(字符串userId)引发IOException{
试一试{
凭证凭证=flow.loadCredential(userId);
if(凭证!=null
&&(credential.getRefreshToken()!=null | | credential.getExpiresInSeconds()>60)){
Userinfoplus Userinfoplus=org.yccheok.jstock.google.Utils.getUserInfo(凭证);
返回新对(新对(凭证、用户InfoPlus),true);
}
//在浏览器中打开
redirectUri=receiver.getRedirectUri();
由于用户将持续使用我的桌面应用程序数天、数周和数月,他们希望只提供一次用户名和密码

有没有办法防止
凭证
过期?我相信这是可以实现的,因为桌面版的Google Drive从不提示我输入用户名和密码。

我们可以通过调用
刷新令牌来“刷新”凭证

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