Java Code Examples for com.google.api.client.googleapis.auth.oauth2.GoogleCredential#refreshToken()

The following examples show how to use com.google.api.client.googleapis.auth.oauth2.GoogleCredential#refreshToken() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GSpreadFeedProcessor.java    From micro-integrator with Apache License 2.0 5 votes vote down vote up
/**
 * helper method to refresh the access token and authenticate
 *
 * @throws Exception
 */
private void refreshAndAuthenticate() throws Exception {
    GoogleCredential credential = getBaseCredential();
    credential.setAccessToken(this.accessToken);
    credential.setRefreshToken(this.refreshToken);
    credential.refreshToken();
    this.accessToken = credential.getAccessToken();
    this.service.setOAuth2Credentials(credential);
}
 
Example 2
Source File: Configure.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve a valid access token that can be use to authorize requests to the Remote Config REST
 * API.
 *
 * @return Access token.
 * @throws IOException
 */
// [START retrieve_access_token]
private static String getAccessToken() throws IOException {
  GoogleCredential googleCredential = GoogleCredential
      .fromStream(new FileInputStream("service-account.json"))
      .createScoped(Arrays.asList(SCOPES));
  googleCredential.refreshToken();
  return googleCredential.getAccessToken();
}
 
Example 3
Source File: Messaging.java    From quickstart-java with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve a valid access token that can be use to authorize requests to the FCM REST
 * API.
 *
 * @return Access token.
 * @throws IOException
 */
// [START retrieve_access_token]
private static String getAccessToken() throws IOException {
  GoogleCredential googleCredential = GoogleCredential
      .fromStream(new FileInputStream("service-account.json"))
      .createScoped(Arrays.asList(SCOPES));
  googleCredential.refreshToken();
  return googleCredential.getAccessToken();
}
 
Example 4
Source File: DefaultDhisConfigurationProvider.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Optional<GoogleAccessToken> getGoogleAccessToken()
{
    if ( !getGoogleCredential().isPresent() )
    {
        return Optional.empty();
    }

    GoogleCredential credential = getGoogleCredential().get();

    try
    {
        if ( !credential.refreshToken() || credential.getExpiresInSeconds() == null )
        {
            log.warn( "There is no refresh token to be retrieved" );

            return Optional.empty();
        }
    }
    catch ( IOException ex )
    {
        throw new IllegalStateException( "Could not retrieve refresh token: " + ex.getMessage(), ex );
    }

    GoogleAccessToken token = new GoogleAccessToken();

    token.setAccessToken( credential.getAccessToken() );
    token.setClientId( getProperty( ConfigurationKey.GOOGLE_SERVICE_ACCOUNT_CLIENT_ID ) );
    token.setExpiresInSeconds( credential.getExpiresInSeconds() );
    token.setExpiresOn( LocalDateTime.now().plusSeconds( token.getExpiresInSeconds() ) );

    return Optional.of( token );
}