Search in sources :

Example 11 with Response

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);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) OAuthException(org.scribe.exceptions.OAuthException) IOException(java.io.IOException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 12 with Response

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);
    }
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Calendar(java.util.Calendar) GregorianCalendar(java.util.GregorianCalendar) GregorianCalendar(java.util.GregorianCalendar) Token(org.scribe.model.Token) IOException(java.io.IOException) Verifier(org.scribe.model.Verifier) Date(java.util.Date) AuthenticationResult(fi.otavanopisto.muikku.auth.AuthenticationResult) Response(org.scribe.model.Response) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 13 with Response

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);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response)

Example 14 with Response

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);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) IOException(java.io.IOException)

Example 15 with Response

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);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) IOException(java.io.IOException)

Aggregations

Response (org.scribe.model.Response)28 OAuthRequest (org.scribe.model.OAuthRequest)26 IOException (java.io.IOException)8 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)6 Date (java.util.Date)5 AuthenticationResult (fi.otavanopisto.muikku.auth.AuthenticationResult)3 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 Token (org.scribe.model.Token)3 Verifier (org.scribe.model.Verifier)3 JsonObject (com.google.gson.JsonObject)2 JsonParser (com.google.gson.JsonParser)2 SimpleDateFormat (java.text.SimpleDateFormat)2 JSONObject (org.json.JSONObject)2 OAuthException (org.scribe.exceptions.OAuthException)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonElement (com.google.gson.JsonElement)1 WhoAmI (fi.otavanopisto.pyramus.rest.model.WhoAmI)1 HashMap (java.util.HashMap)1