use of org.scribe.model.OAuthRequest in project muikku by otavanopisto.
the class GoogleAuthenticationStrategy 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);
GoogleAccessToken googleAccessToken;
try {
googleAccessToken = objectMapper.readValue(accessToken.getRawResponse(), GoogleAccessToken.class);
Calendar calendar = new GregorianCalendar();
calendar.setTime(new Date());
calendar.add(Calendar.SECOND, googleAccessToken.getExpiresIn());
Date expires = calendar.getTime();
sessionController.addOAuthAccessToken("google", expires, accessToken.getToken(), null);
} catch (IOException e) {
logger.log(Level.SEVERE, "Token extraction failed a JSON parsing error", e);
return new AuthenticationResult(AuthenticationResult.Status.ERROR);
}
List<String> scopesList = Arrays.asList(requestedScopes);
boolean hasProfileScope = scopesList.contains("https://www.googleapis.com/auth/userinfo.profile");
GoogleUserInfo userInfo = null;
if (hasProfileScope) {
OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
service.signRequest(accessToken, request);
Response response = request.send();
try {
userInfo = objectMapper.readValue(response.getBody(), GoogleUserInfo.class);
} catch (IOException e) {
logger.log(Level.SEVERE, "Logging in failed because of a JSON parsing exception", e);
return new AuthenticationResult(AuthenticationResult.Status.ERROR);
}
}
if (userInfo != null)
return processLogin(authSource, requestParameters, userInfo.getId(), Arrays.asList(userInfo.getEmail()), userInfo.getGivenName(), userInfo.getFamilyName());
else {
return new AuthenticationResult(AuthenticationResult.Status.GRANT);
}
}
use of org.scribe.model.OAuthRequest 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.OAuthRequest 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.OAuthRequest 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.OAuthRequest in project fitscales by paulburton.
the class FitBitSyncService method syncWeight.
@Override
public boolean syncWeight(float weight) {
try {
SimpleDateFormat tsFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
String timestamp = tsFormat.format(new Date());
OAuthRequest request = new OAuthRequest(Verb.POST, API_BASE + "/user/-/body.json");
request.addBodyParameter("weight", String.format("%.2f", weight));
request.addBodyParameter("date", timestamp);
oaService.signRequest(oaToken, request);
Response response = request.send();
int code = response.getCode();
if (DEBUG) {
Log.d(TAG, "Response code " + code);
try {
Log.d(TAG, "Response body " + response.getBody());
} catch (Exception ex) {
Log.e(TAG, "Response body error", ex);
}
}
if (code == 200 || code == 201 || code == 204)
return true;
return false;
} catch (Exception ex) {
return false;
}
}
Aggregations