com.urbanairship.api.client.OAuthTokenFetcher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-client Show documentation
Show all versions of java-client Show documentation
The Urban Airship Java client library
package com.urbanairship.api.client;
import com.google.common.io.BaseEncoding;
import com.urbanairship.api.client.parse.OAuthTokenResponse;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class OAuthTokenFetcher {
private final HttpClient httpClient;
private final ObjectMapper objectMapper;
public OAuthTokenFetcher() {
this.httpClient = HttpClients.createDefault();
this.objectMapper = new ObjectMapper();
}
public OAuthTokenResponse fetchToken(OAuthCredentials credentials, String baseOAuthURL) throws IOException {
int maxRetries = 3;
int retryCount = 0;
boolean success = false;
String accessToken = null;
long expiresIn = 0;
while (!success && retryCount < maxRetries) {
if (retryCount > 0) {
try {
Thread.sleep(1000 * (long) Math.pow(2, retryCount));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Interrupted while waiting to retry token fetch", e);
}
}
HttpPost post = new HttpPost(baseOAuthURL + "/token");
post.setHeader("Content-Type", "application/x-www-form-urlencoded");
post.setHeader("Accept", "application/json");
if (credentials.getClientSecret().isPresent()) {
String clientId = credentials.getClientId();
String clientSecret = credentials.getClientSecret().get();
String authHeader = BaseEncoding.base64().encode((clientId + ":" + clientSecret).getBytes());
post.setHeader("Authorization", "Basic " + authHeader);
}
List params = credentials.toParams();
post.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String jsonResponse = EntityUtils.toString(response.getEntity());
Map responseMap = objectMapper.readValue(jsonResponse, new TypeReference
© 2015 - 2024 Weber Informatics LLC | Privacy Policy