/** * Request an access token from an OAuth 2.0 provider. * * <p>If it can be determined that the user has already granted access, and the token has not yet * expired, and that the token will not expire soon, the existing token will be passed to the * callback. * * <p>Otherwise, a popup window will be displayed which may prompt the user to grant access. If * the user has already granted access the popup will immediately close and the token will be * passed to the callback. If access hasn't been granted, the user will be prompted, and when they * grant, the token will be passed to the callback. * * @param req Request for authentication. * @param callback Callback to pass the token to when access has been granted. */ public void login(AuthRequest req, final Callback<String, Throwable> callback) { lastRequest = req; lastCallback = callback; String authUrl = req.toUrl(urlCodex) + "&redirect_uri=" + urlCodex.encode(oauthWindowUrl); // Try to look up the token we have stored. final TokenInfo info = getToken(req); if (info == null || info.expires == null || expiringSoon(info)) { // Token wasn't found, or doesn't have an expiration, or is expired or // expiring soon. Requesting access will refresh the token. doLogin(authUrl, callback); } else { // Token was found and is good, immediately execute the callback with the // access token. scheduler.scheduleDeferred( new ScheduledCommand() { @Override public void execute() { callback.onSuccess(info.accessToken); } }); } }