문제

Currently, I'm integrating Google Drive API using oAuth 2.0 with my desktop app.

I realize, after some period of time, the Credential stored on disk will expire. When it happens, I need to prompt the user via the browser again, to ask them to enter their username and password.

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();

As the user will keep using my desktop app for days, weeks and months they are expecting to provide username & password only once.

Is there any way to prevent Credential from expiring? I believe this is something achievable, as the desktop version of Google Drive never prompts me for my username and password.

도움이 되었습니까?

해결책

We can "refresh" the Credential by calling refreshToken.

boolean success = false;
if (credential.getExpiresInSeconds() <= 60) {
    if (credential.refreshToken()) {
        success = true;
    }
} else {
    success = true;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top