use of org.scribe.model.Response in project muikku by otavanopisto.
the class GoogleApi20ServiceImpl method getAccessToken.
@Override
public Token getAccessToken(Token requestToken, Verifier verifier) {
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
request.addBodyParameter(OAuthConstants.CLIENT_ID, config.getApiKey());
request.addBodyParameter(OAuthConstants.CLIENT_SECRET, config.getApiSecret());
request.addBodyParameter(OAuthConstants.CODE, verifier.getValue());
request.addBodyParameter(OAuthConstants.REDIRECT_URI, config.getCallback());
request.addBodyParameter("grant_type", "authorization_code");
if (config.hasScope())
request.addBodyParameter(OAuthConstants.SCOPE, config.getScope());
Response response = request.send();
ObjectMapper objectMapper = new ObjectMapper();
String tokenJson;
try {
tokenJson = objectMapper.writeValueAsString(objectMapper.readTree(response.getBody()));
} catch (IOException e) {
throw new OAuthException("Invalid Token JSON", e);
}
return api.getAccessTokenExtractor().extract(tokenJson);
}
use of org.scribe.model.Response in project muikku by otavanopisto.
the class FacebookAuthenticationStrategy method processResponse.
@Override
protected AuthenticationResult processResponse(AuthSource authSource, Map<String, String[]> requestParameters, OAuthService service, String[] requestedScopes) {
ObjectMapper objectMapper = new ObjectMapper();
String verifier = getFirstRequestParameter(requestParameters, "code");
Verifier v = new Verifier(verifier);
Token accessToken = service.getAccessToken(null, v);
FacebookUser meObject = null;
OAuthRequest request = new OAuthRequest(Verb.GET, "https://graph.facebook.com/me");
service.signRequest(accessToken, request);
Response response = request.send();
try {
meObject = objectMapper.readValue(response.getBody(), FacebookUser.class);
} catch (IOException e) {
logger.log(Level.SEVERE, "Logging in failed because of a JSON parsing exception", e);
return new AuthenticationResult(AuthenticationResult.Status.ERROR);
}
Integer expiresIn = extractExpires(accessToken);
Date expires = null;
if (expiresIn != null) {
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(Calendar.SECOND, expiresIn);
expires = calendar.getTime();
sessionController.addOAuthAccessToken("facebook", expires, accessToken.getToken(), null);
}
if (meObject != null)
return processLogin(authSource, requestParameters, meObject.getId(), Arrays.asList(meObject.getEmail()), meObject.getFirstName(), meObject.getLastName());
else {
return new AuthenticationResult(AuthenticationResult.Status.GRANT);
}
}
use of org.scribe.model.Response in project openolat by klemens.
the class FacebookProvider method getUser.
@Override
public OAuthUser getUser(OAuthService service, Token accessToken) {
OAuthRequest request = new OAuthRequest(Verb.GET, "https://graph.facebook.com/me");
service.signRequest(accessToken, request);
Response oauthResponse = request.send();
String body = oauthResponse.getBody();
return parseInfos(body);
}
use of org.scribe.model.Response in project data-transfer-project by google.
the class SmugMugInterface method postRequest.
// Makes a post request with the content parameters provided as the body, or the httpcontent as
// the body
private <T> T postRequest(String url, Map<String, String> contentParams, @Nullable byte[] contentBytes, Map<String, String> smugMugHeaders, TypeReference<T> typeReference) throws IOException {
String fullUrl = url;
if (!fullUrl.contains("://")) {
fullUrl = BASE_URL + url;
}
OAuthRequest request = new OAuthRequest(Verb.POST, fullUrl);
// Add payload
if (contentBytes != null) {
request.addPayload(contentBytes);
}
// Add body params
for (Entry<String, String> param : contentParams.entrySet()) {
request.addBodyParameter(param.getKey(), param.getValue());
}
// sign request before adding any of the headers since those shouldn't be included in the
// signature
oAuthService.signRequest(accessToken, request);
// Add headers
for (Entry<String, String> header : smugMugHeaders.entrySet()) {
request.addHeader(header.getKey(), header.getValue());
}
// add accept and content type headers so the response comes back in json and not html
request.addHeader(HttpHeaders.ACCEPT, "application/json");
Response response = request.send();
if (response.getCode() < 200 || response.getCode() >= 300) {
if (response.getCode() == 400) {
throw new IOException(String.format("Error occurred in request for %s, code: %s, message: %s, request: %s, bodyParams: %s, payload: %s", fullUrl, response.getCode(), response.getMessage(), request, request.getBodyParams(), request.getBodyContents()));
}
throw new IOException(String.format("Error occurred in request for %s, code: %s, message: %s", fullUrl, response.getCode(), response.getMessage()));
}
return mapper.readValue(response.getBody(), typeReference);
}
use of org.scribe.model.Response in project data-transfer-project by google.
the class SmugMugInterface method makeRequest.
private <T> SmugMugResponse<T> makeRequest(String url, TypeReference<SmugMugResponse<T>> typeReference) throws IOException {
// Note: there are no request params that need to go here, because smugmug fully specifies
// which resource to get in the URL of a request, without using query params.
String fullUrl;
if (!url.contains("https://")) {
fullUrl = BASE_URL + url;
} else {
fullUrl = url;
}
fullUrl += (url.contains("?") ? "&" : "?") + "_accept=application%2Fjson";
OAuthRequest request = new OAuthRequest(Verb.GET, fullUrl);
oAuthService.signRequest(accessToken, request);
final Response response = request.send();
if (response.getCode() < 200 || response.getCode() >= 300) {
throw new IOException(String.format("Error occurred in request for %s : %s", url, response.getMessage()));
}
return mapper.readValue(response.getBody(), typeReference);
}
Aggregations