Search in sources :

Example 1 with OAuthRequest

use of org.scribe.model.OAuthRequest in project openhab1-addons by openhab.

the class OpenPathsBinding method getUserLocation.

@SuppressWarnings("unchecked")
private Location getUserLocation(String accessKey, String secretKey) {
    // build the OAuth service using the access/secret keys
    OAuthService service = new ServiceBuilder().provider(new OpenPathsApi()).apiKey(accessKey).apiSecret(secretKey).build();
    // build the request
    OAuthRequest request = new OAuthRequest(Verb.GET, "https://openpaths.cc/api/1");
    service.signRequest(Token.empty(), request);
    request.addQuerystringParameter("num_points", "1");
    // send the request and check we got a successful response
    Response response = request.send();
    if (!response.isSuccessful()) {
        logger.error("Failed to request the OpenPaths location, response code: " + response.getCode());
        return null;
    }
    // parse the response to build our location object
    Map<String, Object> locationData;
    String toParse = "{}";
    try {
        ObjectMapper jsonReader = new ObjectMapper();
        toParse = response.getBody();
        toParse = toParse.substring(1, toParse.length() - 2);
        locationData = jsonReader.readValue(toParse, Map.class);
    } catch (JsonParseException e) {
        logger.error("Error parsing JSON:\n" + toParse, e);
        return null;
    } catch (JsonMappingException e) {
        logger.error("Error mapping JSON:\n" + toParse, e);
        return null;
    } catch (IOException e) {
        logger.error("An I/O error occured while decoding JSON:\n" + response.getBody());
        return null;
    }
    float latitude = Float.parseFloat(locationData.get("lat").toString());
    float longitude = Float.parseFloat(locationData.get("lon").toString());
    String device = locationData.get("device").toString();
    return new Location(latitude, longitude, device);
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) IOException(java.io.IOException) JsonParseException(org.codehaus.jackson.JsonParseException) ServiceBuilder(org.scribe.builder.ServiceBuilder) Response(org.scribe.model.Response) OAuthService(org.scribe.oauth.OAuthService) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Example 2 with OAuthRequest

use of org.scribe.model.OAuthRequest in project camel by apache.

the class ScribeApiRequestor method send.

private String send(Verb verb, String params) throws Exception {
    String url = apiUrl + ((params != null) ? params : "");
    OAuthRequest request = new OAuthRequest(verb, url);
    request.addQuerystringParameter(OAuthConstants.ACCESS_TOKEN, apiAccessToken);
    // For more details on the “Bearer” token refer to http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-23
    StringBuilder sb = new StringBuilder();
    sb.append("Bearer ");
    sb.append(apiAccessToken);
    request.addHeader("Authorization", sb.toString());
    if (LOG.isDebugEnabled()) {
        LOG.debug("Yammer request url: {}", request.getCompleteUrl());
    }
    Response response = request.send();
    if (response.isSuccessful()) {
        return response.getBody();
    } else {
        throw new Exception(String.format("Failed to poll %s. Got response code %s and body: %s", getApiUrl(), response.getCode(), response.getBody()));
    }
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response)

Example 3 with OAuthRequest

use of org.scribe.model.OAuthRequest in project fitscales by paulburton.

the class FitBitSyncService method postOAuth.

@Override
protected void postOAuth() {
    try {
        OAuthRequest request = new OAuthRequest(Verb.GET, API_BASE + "/user/-/profile.json");
        oaService.signRequest(oaToken, request);
        Response response = request.send();
        if (DEBUG)
            Log.d(TAG, "Profile response code " + response.getCode());
        String body = response.getBody();
        if (DEBUG)
            Log.d(TAG, "Got profile body " + body);
        JSONObject json = new JSONObject(body);
        user = json.getJSONObject("user").getString("displayName");
    } catch (Exception ex) {
        if (DEBUG)
            Log.e(TAG, "Failed to get profile", ex);
        user = "unknown";
    }
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) JSONObject(org.json.JSONObject)

Example 4 with OAuthRequest

use of org.scribe.model.OAuthRequest in project fitscales by paulburton.

the class RunKeeperSyncService method syncWeight.

@Override
public boolean syncWeight(float weight) {
    try {
        SimpleDateFormat tsFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.ENGLISH);
        String timestamp = tsFormat.format(new Date());
        String json = String.format("{\"weight\": %.2f,\"timestamp\": \"%s\" }", weight, timestamp);
        if (DEBUG)
            Log.d(TAG, "Posting weight " + json);
        OAuthRequest request = new OAuthRequest(Verb.POST, API_BASE + "/weight");
        request.addPayload(json);
        request.addHeader("Content-Type", "application/vnd.com.runkeeper.NewWeight+json");
        oaService.signRequest(oaToken, request);
        Response response = request.send();
        int code = response.getCode();
        if (DEBUG)
            Log.d(TAG, "Response code " + code);
        if (code == 200 || code == 201 || code == 204)
            return true;
        return false;
    } catch (Exception ex) {
        return false;
    }
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 5 with OAuthRequest

use of org.scribe.model.OAuthRequest in project mamute by caelum.

the class FacebookAPI method makeRequest.

private Response makeRequest(String url) {
    OAuthRequest request = new OAuthRequest(Verb.GET, url);
    service.signRequest(accessToken, request);
    Response response = request.send();
    String body = response.getBody();
    if (response.getCode() / 100 != 2) {
        throw new IllegalArgumentException("http error: " + response.getCode() + ", facebook response body: " + body);
    }
    return response;
}
Also used : OAuthRequest(org.scribe.model.OAuthRequest) Response(org.scribe.model.Response)

Aggregations

OAuthRequest (org.scribe.model.OAuthRequest)24 Response (org.scribe.model.Response)23 IOException (java.io.IOException)6 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 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 WhoAmI (fi.otavanopisto.pyramus.rest.model.WhoAmI)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Users (org.apache.sling.auth.xing.api.users.Users)1