Question

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.

Était-ce utile?

La solution

We can "refresh" the Credential by calling refreshToken.

boolean success = false;
if (credential.getExpiresInSeconds() <= 60) {
    if (credential.refreshToken()) {
        success = true;
    }
} else {
    success = true;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top