/** * Get an accessToken to use, possibly refreshing the token if it expires within the * minimumExpiryMillis. */ private AccessToken getAccessToken() throws IOException { // synchronize attempts to refresh the accessToken synchronized (credentials) { if (needsRefresh(credentials.getAccessToken())) { credentialRefresher.refresh(credentials); } } return credentials.getAccessToken(); }
@Override public Optional<AccessToken> get() { Optional<AccessToken> tokenOpt = Optional.absent(); if (enabled) { if (staticToken != null) { tokenOpt = Optional.of(staticToken); } else { try { synchronized (lock) { if (credentials == null) { credentials = getCredentialsWithScopes(tokenScopes); } credentials.refreshIfExpired(); } tokenOpt = Optional.of(credentials.getAccessToken()); } catch (IOException | RuntimeException e) { LOG.debug("Exception (possibly benign) while loading Google Credentials", e); return Optional.absent(); } } } return tokenOpt; }
private boolean needsRefresh(final AccessToken accessToken) { if (accessToken == null) { // has not yet been fetched return true; } final Date expirationTime = credentials.getAccessToken().getExpirationTime(); // Don't refresh if expiration time hasn't been provided. if (expirationTime == null) { return false; } // refresh the token if it expires "soon" final long expiresIn = expirationTime.getTime() - clock.millis(); return expiresIn <= minimumExpiryMillis; }
protected Builder(GoogleCredentials credentials) { setAccessToken(credentials.getAccessToken()); }
protected Builder(GoogleCredentials credentials) { setAccessToken(credentials.getAccessToken()); }
/** * Starts the TokenRefresher if not already started. Starts listening to credentials changed * events, and schedules refresh events every time the OAuth2 token changes. If no active * token is present, or if the available token is set to expire soon, this will also schedule * a refresh event to be executed immediately. * * <p>This operation is idempotent. Calling it multiple times, or calling it after the * refresher has been stopped has no effect. */ final synchronized void start() { // Allow starting only from the ready state. if (!state.compareAndSet(State.READY, State.STARTED)) { return; } logger.debug("Starting the proactive token refresher"); credentials.addChangeListener(this); AccessToken accessToken = credentials.getAccessToken(); long refreshDelay; if (accessToken != null) { // If the token is about to expire (i.e. expires in less than 5 minutes), schedule a // refresh event with 0 delay. Otherwise schedule a refresh event at the regular token // expiry time, minus 5 minutes. refreshDelay = Math.max(getRefreshDelay(accessToken), 0L); } else { // If there is no token fetched so far, fetch one immediately. refreshDelay = 0L; } scheduleRefresh(refreshDelay); }
@Override public void getToken(boolean forceRefresh, final GetTokenCompletionListener listener) { try { if (forceRefresh) { credentials.refresh(); } // The typical way to use a GoogleCredentials instance is to call its getRequestMetadata(), // and include the metadata in your request. Since we are accessing the token directly via // getAccessToken(), we must first call getRequestMetadata() to ensure the token is available // (refreshed if necessary). credentials.getRequestMetadata(); AccessToken accessToken = credentials.getAccessToken(); listener.onSuccess(wrapOAuthToken(accessToken, authVariable)); } catch (Exception e) { listener.onError(e.toString()); } }
@Override public AccessToken refreshAccessToken() throws IOException { if (this.sourceCredentials.getAccessToken() == null) { this.sourceCredentials = this.sourceCredentials .createScoped(Arrays.asList(CLOUD_PLATFORM_SCOPE));
@Override public AccessToken refreshAccessToken() throws IOException { if (this.sourceCredentials.getAccessToken() == null) { this.sourceCredentials = this.sourceCredentials .createScoped(Arrays.asList(CLOUD_PLATFORM_SCOPE));