edu.ksu.canvas.oauth.OauthTokenRefresher Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of canvas-api Show documentation
Show all versions of canvas-api Show documentation
A native Java library to talk to the Canvas REST API
package edu.ksu.canvas.oauth;
import java.io.IOException;
import java.io.Serializable;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import edu.ksu.canvas.impl.GsonResponseParser;
public class OauthTokenRefresher implements Serializable {
private static final long serialVersionUID = 1L;
private static final Logger LOG = Logger.getLogger(OauthTokenRefresher.class);
private static final int TIMEOUT_SECONDS = 10;
private final String clientId;
private final String clientSecret;
private final String canvasUrl;
public OauthTokenRefresher(String clientId, String clientSecret, String canvasUrl) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.canvasUrl = canvasUrl;
}
public TokenRefreshResponse getNewToken(String refreshToken) throws IOException {
LOG.debug("Getting a fresh OAuth access token");
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(TIMEOUT_SECONDS*1000)
.setSocketTimeout(TIMEOUT_SECONDS*1000)
.build();
CloseableHttpClient httpClient = HttpClientBuilder.create()
.setDefaultRequestConfig(config)
.build();
String url = canvasUrl + "/login/oauth2/token?grant_type=refresh_token&client_id=" + clientId + "&client_secret=" + clientSecret + "&refresh_token=" + refreshToken;
HttpPost postRequest = new HttpPost(url);
try {
HttpResponse httpResponse = httpClient.execute(postRequest);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == 401) {
LOG.error("Unauthorized refresh token request. Wrong client_id or secret?");
return null;
}
if (statusCode != 200) {
LOG.error("Non-200 status code ( " + statusCode + " )returned while requesting an access token at URL " + url);
HttpEntity errorEntity = httpResponse.getEntity();
if (errorEntity != null) {
String errorBody = EntityUtils.toString(errorEntity);
LOG.error("Response from Canvas: " + errorBody);
}
return null;
}
HttpEntity entity = httpResponse.getEntity();
String responseBody = EntityUtils.toString(entity);
Gson gson = GsonResponseParser.getDefaultGsonParser(false);
return gson.fromJson(responseBody, TokenRefreshResponse.class);
} finally {
postRequest.releaseConnection();
httpClient.close();
}
}
}