use of org.scribe.model.Response 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);
}
use of org.scribe.model.Response 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()));
}
}
use of org.scribe.model.Response 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";
}
}
use of org.scribe.model.Response 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;
}
}
use of org.scribe.model.Response in project mamute by caelum.
the class FacebookAPI method getUserId.
public String getUserId() {
String url = "https://graph.facebook.com/me?fields=id";
Response response = makeRequest(url);
String body = response.getBody();
JsonObject jsonObj = new JsonParser().parse(body).getAsJsonObject();
JsonElement jsonElement = jsonObj.get("id");
if (jsonElement == null) {
throw new IllegalArgumentException("facebook did not sent data requested! response body: " + body);
}
return jsonElement.getAsString();
}
Aggregations