Search in sources :

Example 1 with OAuthException

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();
}
Also used : OAuthException(oauth.signpost.exception.OAuthException) OAuthConsumer(oauth.signpost.OAuthConsumer)

Example 2 with OAuthException

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;
}
Also used : HttpRequest(oauth.signpost.http.HttpRequest) OAuthException(oauth.signpost.exception.OAuthException) RescuOAuthRequestAdapter(si.mazi.rescu.oauth.RescuOAuthRequestAdapter)

Example 3 with OAuthException

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);
    }
}
Also used : MeasureResult(org.openhab.binding.withings.internal.model.MeasureResult) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) OAuthException(oauth.signpost.exception.OAuthException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 4 with OAuthException

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);
    }
}
Also used : OAuthException(oauth.signpost.exception.OAuthException) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) OAuthConsumer(oauth.signpost.OAuthConsumer)

Example 5 with OAuthException

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));
}
Also used : ContinuationInformation(org.dataportabilityproject.dataModels.ContinuationInformation) PhotoModel(org.dataportabilityproject.dataModels.photos.PhotoModel) SmugMugAlbumImage(org.dataportabilityproject.serviceProviders.smugmug.model.SmugMugAlbumImage) OAuthException(oauth.signpost.exception.OAuthException) ArrayList(java.util.ArrayList) PhotosModelWrapper(org.dataportabilityproject.dataModels.photos.PhotosModelWrapper) IOException(java.io.IOException) StringPaginationToken(org.dataportabilityproject.shared.StringPaginationToken) SmugMugAlbumInfoResponse(org.dataportabilityproject.serviceProviders.smugmug.model.SmugMugAlbumInfoResponse)

Aggregations

OAuthException (oauth.signpost.exception.OAuthException)5 IOException (java.io.IOException)2 OAuthConsumer (oauth.signpost.OAuthConsumer)2 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 ArrayList (java.util.ArrayList)1 DefaultOAuthProvider (oauth.signpost.basic.DefaultOAuthProvider)1 HttpRequest (oauth.signpost.http.HttpRequest)1 ContinuationInformation (org.dataportabilityproject.dataModels.ContinuationInformation)1 PhotoModel (org.dataportabilityproject.dataModels.photos.PhotoModel)1 PhotosModelWrapper (org.dataportabilityproject.dataModels.photos.PhotosModelWrapper)1 SmugMugAlbumImage (org.dataportabilityproject.serviceProviders.smugmug.model.SmugMugAlbumImage)1 SmugMugAlbumInfoResponse (org.dataportabilityproject.serviceProviders.smugmug.model.SmugMugAlbumInfoResponse)1 StringPaginationToken (org.dataportabilityproject.shared.StringPaginationToken)1 MeasureResult (org.openhab.binding.withings.internal.model.MeasureResult)1 RescuOAuthRequestAdapter (si.mazi.rescu.oauth.RescuOAuthRequestAdapter)1