use of oauth.signpost.exception.OAuthException in project openhab1-addons by openhab.
the class WithingsAuthenticator method finishAuthentication.
/**
* Finishes the OAuth authentication flow.
*
* @param verificationCode
* OAuth verification code
* @param userId
* user id
*/
public synchronized void finishAuthentication(String accountId, String verificationCode, String userId) {
WithingsAccount withingsAccount = getAccount(accountId);
if (withingsAccount == null) {
logger.warn("Couldn't find Credentials of Account '{}'. Please check openhab.cfg or withings.cfg.", accountId);
return;
}
OAuthConsumer consumer = withingsAccount.consumer;
if (provider == null || consumer == null) {
logger.warn("Could not finish authentication. Please execute 'startAuthentication' first.");
return;
}
try {
provider.retrieveAccessToken(consumer, verificationCode);
} catch (OAuthException ex) {
logger.error(ex.getMessage(), ex);
printAuthenticationFailed(ex);
return;
}
withingsAccount.userId = userId;
withingsAccount.setOuathToken(consumer.getToken(), consumer.getTokenSecret());
withingsAccount.registerAccount(componentContext.getBundleContext());
withingsAccount.persist();
printAuthenticationSuccessful();
}
use of oauth.signpost.exception.OAuthException in project rescu by mmazi.
the class HttpTemplate method send.
HttpURLConnection send(String urlString, String requestBody, Map<String, String> httpHeaders, HttpMethod method) throws IOException {
log.debug("Executing {} request at {}", method, urlString);
log.trace("Request body = {}", requestBody);
log.trace("Request headers = {}", httpHeaders);
preconditionNotNull(urlString, "urlString cannot be null");
preconditionNotNull(httpHeaders, "httpHeaders should not be null");
int contentLength = requestBody == null ? 0 : requestBody.getBytes().length;
HttpURLConnection connection = configureURLConnection(method, urlString, httpHeaders, contentLength);
if (oAuthConsumer != null) {
HttpRequest request = new RescuOAuthRequestAdapter(connection, requestBody);
try {
oAuthConsumer.sign(request);
} catch (OAuthException e) {
throw new RuntimeException("OAuth error", e);
}
}
if (contentLength > 0) {
// Write the request body
OutputStream out = connection.getOutputStream();
out.write(requestBody.getBytes(CHARSET_UTF_8));
out.flush();
}
return connection;
}
use of oauth.signpost.exception.OAuthException in project openhab1-addons by openhab.
the class WithingsApiClient method getMeasures.
/**
* Returns a list of all measures in form {@link MeasureGroup} objects since
* the given start time.
*
* @param startTime
* time after which measures are returned
* @return list of all measures
* @throws OAuthException
* if an error occurs while signing the request
* @throws WithingsConnectionException
* if a connection, server or authorization error occurs
* @see http://www.withings.com/de/api#bodymetrics
*/
public List<MeasureGroup> getMeasures(int startTime) throws OAuthException, WithingsConnectionException {
String url = getServiceUrl(API_ENDPOINT_MEASURE, API_METHOD_GET_MEASURES);
if (startTime > 0) {
url = url + "&startdate=" + startTime;
}
try {
JsonObject jsonObject = call(consumer.sign(url));
int status = jsonObject.get("status").getAsInt();
if (status == 0) {
JsonElement body = jsonObject.get("body");
return gson.fromJson(body.getAsJsonObject(), MeasureResult.class).measureGroups;
} else {
throw new WithingsConnectionException("Withings API call failed: " + status);
}
} catch (Exception ex) {
throw new WithingsConnectionException("Could not connect to URL: " + ex.getMessage(), ex);
}
}
use of oauth.signpost.exception.OAuthException in project openhab1-addons by openhab.
the class WithingsAuthenticator method startAuthentication.
/**
* Starts the OAuth authentication flow.
*/
public synchronized void startAuthentication(String accountId) {
WithingsAccount withingsAccount = getAccount(accountId);
if (withingsAccount == null) {
logger.warn("Couldn't find Credentials of Account '{}'. Please check openhab.cfg or withings.cfg.", accountId);
return;
}
OAuthConsumer consumer = withingsAccount.createConsumer();
provider = new DefaultOAuthProvider(OAUTH_REQUEST_TOKEN_ENDPOINT, OAUTH_ACCESS_TOKEN_ENDPOINT_URL, OAUTH_AUTHORIZE_ENDPOINT_URL);
try {
String url = provider.retrieveRequestToken(consumer, this.redirectUrl);
printSetupInstructions(url);
} catch (OAuthException ex) {
logger.error(ex.getMessage(), ex);
printAuthenticationFailed(ex);
}
}
use of oauth.signpost.exception.OAuthException in project data-transfer-project by google.
the class SmugMugPhotoService method getImages.
private PhotosModelWrapper getImages(IdOnlyResource resource, Optional<PaginationInformation> paginationInformation) throws IOException {
List<PhotoModel> photos = new ArrayList<>();
String url;
if (paginationInformation.isPresent()) {
url = ((StringPaginationToken) paginationInformation.get()).getId();
} else {
String id = resource.getId();
url = "/api/v2/album/" + id + "!images";
}
StringPaginationToken pageToken = null;
SmugMugResponse<SmugMugAlbumInfoResponse> albumInfoResponse = makeAlbumInfoRequest(url);
if (albumInfoResponse.getResponse().getImages() != null) {
for (SmugMugAlbumImage image : albumInfoResponse.getResponse().getImages()) {
String title = image.getTitle();
if (Strings.isNullOrEmpty(title)) {
title = image.getFileName();
}
try {
photos.add(new PhotoModel(title, this.authConsumer.sign(image.getArchivedUri()), image.getCaption(), image.getFormat(), resource.getId()));
} catch (OAuthException e) {
throw new IOException("Couldn't sign: " + image.getArchivedUri(), e);
}
}
if (albumInfoResponse.getResponse().getPageInfo().getNextPage() != null) {
pageToken = new StringPaginationToken(albumInfoResponse.getResponse().getPageInfo().getNextPage());
}
}
return new PhotosModelWrapper(null, photos, new ContinuationInformation(null, pageToken));
}
Aggregations