Search in sources :

Example 1 with BackOffRequired

use of com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired in project google-auth-library-java by google.

the class ServiceAccountCredentials method refreshAccessToken.

/**
 * Refreshes the OAuth2 access token by getting a new access token using a JSON Web Token (JWT).
 */
@Override
public AccessToken refreshAccessToken() throws IOException {
    if (createScopedRequired()) {
        throw new IOException("Scopes not configured for service account. Scoped should be specified" + " by calling createScoped or passing scopes to constructor.");
    }
    JsonFactory jsonFactory = OAuth2Utils.JSON_FACTORY;
    long currentTime = clock.currentTimeMillis();
    String assertion = createAssertion(jsonFactory, currentTime);
    GenericData tokenRequest = new GenericData();
    tokenRequest.set("grant_type", GRANT_TYPE);
    tokenRequest.set("assertion", assertion);
    UrlEncodedContent content = new UrlEncodedContent(tokenRequest);
    HttpRequestFactory requestFactory = transportFactory.create().createRequestFactory();
    HttpRequest request = requestFactory.buildPostRequest(new GenericUrl(tokenServerUri), content);
    request.setParser(new JsonObjectParser(jsonFactory));
    request.setIOExceptionHandler(new HttpBackOffIOExceptionHandler(new ExponentialBackOff()));
    request.setUnsuccessfulResponseHandler(new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff()).setBackOffRequired(new BackOffRequired() {

        public boolean isRequired(HttpResponse response) {
            int code = response.getStatusCode();
            return (// Server error --- includes timeout errors, which use 500 instead of 408
            code / 100 == 5 || // https://github.com/google/google-api-java-client/issues/662
            code == 403);
        }
    }));
    HttpResponse response;
    try {
        response = request.execute();
    } catch (IOException e) {
        throw new IOException("Error getting access token for service account: ", e);
    }
    GenericData responseData = response.parseAs(GenericData.class);
    String accessToken = OAuth2Utils.validateString(responseData, "access_token", PARSE_ERROR_PREFIX);
    int expiresInSeconds = OAuth2Utils.validateInt32(responseData, "expires_in", PARSE_ERROR_PREFIX);
    long expiresAtMilliseconds = clock.currentTimeMillis() + expiresInSeconds * 1000L;
    return new AccessToken(accessToken, new Date(expiresAtMilliseconds));
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpBackOffIOExceptionHandler(com.google.api.client.http.HttpBackOffIOExceptionHandler) HttpBackOffUnsuccessfulResponseHandler(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) JsonFactory(com.google.api.client.json.JsonFactory) UrlEncodedContent(com.google.api.client.http.UrlEncodedContent) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) GenericUrl(com.google.api.client.http.GenericUrl) BackOffRequired(com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired) GenericData(com.google.api.client.util.GenericData) ExponentialBackOff(com.google.api.client.util.ExponentialBackOff) Date(java.sql.Date) JsonObjectParser(com.google.api.client.json.JsonObjectParser)

Aggregations

GenericUrl (com.google.api.client.http.GenericUrl)1 HttpBackOffIOExceptionHandler (com.google.api.client.http.HttpBackOffIOExceptionHandler)1 HttpBackOffUnsuccessfulResponseHandler (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler)1 BackOffRequired (com.google.api.client.http.HttpBackOffUnsuccessfulResponseHandler.BackOffRequired)1 HttpRequest (com.google.api.client.http.HttpRequest)1 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)1 HttpResponse (com.google.api.client.http.HttpResponse)1 UrlEncodedContent (com.google.api.client.http.UrlEncodedContent)1 JsonFactory (com.google.api.client.json.JsonFactory)1 JsonObjectParser (com.google.api.client.json.JsonObjectParser)1 ExponentialBackOff (com.google.api.client.util.ExponentialBackOff)1 GenericData (com.google.api.client.util.GenericData)1 IOException (java.io.IOException)1 Date (java.sql.Date)1