Search in sources :

Example 1 with OAuthAsyncRequestCallback

use of com.github.scribejava.core.model.OAuthAsyncRequestCallback in project twittered by redouane59.

the class RequestHelperV2 method getAsyncRequest.

public <T> Future<Response> getAsyncRequest(String url, Map<String, String> parameters, IAPIEventListener listener, final Class<? extends T> targetClass) {
    if (parameters != null) {
        url += parameters.entrySet().stream().map(p -> p.getKey() + "=" + p.getValue()).reduce((p1, p2) -> p1 + "&" + p2).map(s -> "?" + s).orElse("");
    }
    OAuthRequest request = new OAuthRequest(Verb.GET, url);
    signRequest(request);
    return getService().execute(request, new OAuthAsyncRequestCallback<Response>() {

        @Override
        public void onThrowable(Throwable t) {
            LOGGER.error(t.getMessage(), t);
        }

        @Override
        public void onCompleted(Response response) {
            try {
                tweetStreamConsumer.consumeStream(listener, response, targetClass);
            } catch (Exception e) {
                onThrowable(e);
            }
        }
    });
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BearerToken(io.github.redouane59.twitter.dto.others.BearerToken) Scope(io.github.redouane59.twitter.signature.Scope) SneakyThrows(lombok.SneakyThrows) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) Tweet(io.github.redouane59.twitter.dto.tweet.Tweet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) Map(java.util.Map) IAPIEventListener(io.github.redouane59.twitter.IAPIEventListener) NoSuchElementException(java.util.NoSuchElementException) OAuthAsyncRequestCallback(com.github.scribejava.core.model.OAuthAsyncRequestCallback) OAuthConstants(com.github.scribejava.core.model.OAuthConstants) URIBuilder(org.apache.http.client.utils.URIBuilder) Verb(com.github.scribejava.core.model.Verb) Collectors(java.util.stream.Collectors) Consumer(java.util.function.Consumer) TweetV2(io.github.redouane59.twitter.dto.tweet.TweetV2) Base64(java.util.Base64) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) Entry(java.util.Map.Entry) Optional(java.util.Optional) TwitterCredentials(io.github.redouane59.twitter.signature.TwitterCredentials) NameValuePair(org.apache.http.NameValuePair) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with OAuthAsyncRequestCallback

use of com.github.scribejava.core.model.OAuthAsyncRequestCallback in project android-oauth-handler by codepath.

the class OAuthTokenClient method fetchAccessToken.

// Get the access token by exchanging the requestToken to the defined URL
// Once receiving the access token, fires the onReceivedAccessToken method on the handler
public void fetchAccessToken(final Token requestToken, final Uri uri) {
    Uri authorizedUri = uri;
    if (service.getVersion() == "1.0") {
        if (authorizedUri.getQuery().contains(OAuthConstants.VERIFIER)) {
            String oauth_verifier = authorizedUri.getQueryParameter(OAuthConstants.VERIFIER);
            OAuth1RequestToken oAuth1RequestToken = (OAuth1RequestToken) requestToken;
            OAuth10aService oAuth10aService = (OAuth10aService) service;
            oAuth10aService.getAccessTokenAsync(oAuth1RequestToken, oauth_verifier, new OAuthAsyncRequestCallback<OAuth1AccessToken>() {

                @Override
                public void onCompleted(OAuth1AccessToken oAuth1AccessToken) {
                    setAccessToken(oAuth1AccessToken);
                    handler.onReceivedAccessToken(oAuth1AccessToken, service.getVersion());
                }

                @Override
                public void onThrowable(Throwable e) {
                    handler.onFailure(new OAuthException(e.getMessage()));
                }
            });
        } else {
            // verifier was null
            throw new OAuthException("No verifier code was returned with uri '" + uri + "' " + "and access token cannot be retrieved");
        }
    } else if (service.getVersion() == "2.0") {
        if (authorizedUri.getQuery().contains(OAuthConstants.CODE)) {
            String code = authorizedUri.getQueryParameter(OAuthConstants.CODE);
            OAuth20Service oAuth20Service = (OAuth20Service) service;
            oAuth20Service.getAccessToken(code, new OAuthAsyncRequestCallback<OAuth2AccessToken>() {

                @Override
                public void onCompleted(OAuth2AccessToken accessToken) {
                    setAccessToken(accessToken);
                    handler.onReceivedAccessToken(accessToken, service.getVersion());
                }

                @Override
                public void onThrowable(Throwable t) {
                }
            });
        } else {
            // verifier was null
            handler.onFailure(new OAuthException("No code was returned with uri '" + uri + "' " + "and access token cannot be retrieved"));
        }
    }
}
Also used : OAuthAsyncRequestCallback(com.github.scribejava.core.model.OAuthAsyncRequestCallback) OAuth1AccessToken(com.github.scribejava.core.model.OAuth1AccessToken) OAuth1RequestToken(com.github.scribejava.core.model.OAuth1RequestToken) OAuth2AccessToken(com.github.scribejava.core.model.OAuth2AccessToken) OAuthException(com.github.scribejava.core.exceptions.OAuthException) OAuth10aService(com.github.scribejava.core.oauth.OAuth10aService) Uri(android.net.Uri) OAuth20Service(com.github.scribejava.core.oauth.OAuth20Service)

Example 3 with OAuthAsyncRequestCallback

use of com.github.scribejava.core.model.OAuthAsyncRequestCallback in project scribejava by scribejava.

the class ArmeriaHttpClient method doExecuteAsync.

private <T> CompletableFuture<T> doExecuteAsync(String userAgent, Map<String, String> headers, Verb httpVerb, String completeUrl, Supplier<HttpData> contentSupplier, OAuthAsyncRequestCallback<T> callback, OAuthRequest.ResponseConverter<T> converter) {
    // Get the URI and Path
    final URI uri = URI.create(completeUrl);
    final String path = getServicePath(uri);
    // Fetch/Create WebClient instance for a given Endpoint
    final WebClient client = getClient(uri);
    // Build HTTP request
    final RequestHeadersBuilder headersBuilder = RequestHeaders.of(getHttpMethod(httpVerb), path).toBuilder();
    headersBuilder.add(headers.entrySet());
    if (userAgent != null) {
        headersBuilder.add(OAuthConstants.USER_AGENT_HEADER_NAME, userAgent);
    }
    // Build the request body and execute HTTP request
    final HttpResponse response;
    if (httpVerb.isPermitBody()) {
        // POST, PUT, PATCH and DELETE methods
        final HttpData contents = contentSupplier.get();
        if (httpVerb.isRequiresBody() && contents == null) {
            // POST or PUT methods
            throw new IllegalArgumentException("Contents missing for request method " + httpVerb.name());
        }
        if (headersBuilder.contentType() == null) {
            headersBuilder.contentType(MediaType.FORM_DATA);
        }
        if (contents != null) {
            response = client.execute(headersBuilder.build(), contents);
        } else {
            response = client.execute(headersBuilder.build());
        }
    } else {
        response = client.execute(headersBuilder.build());
    }
    // Aggregate HTTP response (asynchronously) and return the result Future
    return response.aggregate().thenApply(aggregatedResponse -> whenResponseComplete(callback, converter, aggregatedResponse)).exceptionally(throwable -> completeExceptionally(callback, throwable));
}
Also used : RequestHeaders(com.linecorp.armeria.common.RequestHeaders) MultipartPayload(com.github.scribejava.core.httpclient.multipart.MultipartPayload) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Supplier(java.util.function.Supplier) MediaType(com.linecorp.armeria.common.MediaType) AbstractAsyncOnlyHttpClient(com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient) RequestHeadersBuilder(com.linecorp.armeria.common.RequestHeadersBuilder) Future(java.util.concurrent.Future) HttpStatus(com.linecorp.armeria.common.HttpStatus) WebClient(com.linecorp.armeria.client.WebClient) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) URI(java.net.URI) HttpData(com.linecorp.armeria.common.HttpData) OAuthAsyncRequestCallback(com.github.scribejava.core.model.OAuthAsyncRequestCallback) HttpResponse(com.linecorp.armeria.common.HttpResponse) OAuthConstants(com.github.scribejava.core.model.OAuthConstants) Files(java.nio.file.Files) Verb(com.github.scribejava.core.model.Verb) IOException(java.io.IOException) HttpMethod(com.linecorp.armeria.common.HttpMethod) File(java.io.File) Lock(java.util.concurrent.locks.Lock) OAuthRequest(com.github.scribejava.core.model.OAuthRequest) Response(com.github.scribejava.core.model.Response) MultipartUtils(com.github.scribejava.core.httpclient.multipart.MultipartUtils) InputStream(java.io.InputStream) HttpData(com.linecorp.armeria.common.HttpData) AggregatedHttpResponse(com.linecorp.armeria.common.AggregatedHttpResponse) HttpResponse(com.linecorp.armeria.common.HttpResponse) RequestHeadersBuilder(com.linecorp.armeria.common.RequestHeadersBuilder) URI(java.net.URI) WebClient(com.linecorp.armeria.client.WebClient)

Aggregations

OAuthAsyncRequestCallback (com.github.scribejava.core.model.OAuthAsyncRequestCallback)3 OAuthConstants (com.github.scribejava.core.model.OAuthConstants)2 OAuthRequest (com.github.scribejava.core.model.OAuthRequest)2 Response (com.github.scribejava.core.model.Response)2 Verb (com.github.scribejava.core.model.Verb)2 OAuth10aService (com.github.scribejava.core.oauth.OAuth10aService)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Future (java.util.concurrent.Future)2 Uri (android.net.Uri)1 OAuthException (com.github.scribejava.core.exceptions.OAuthException)1 AbstractAsyncOnlyHttpClient (com.github.scribejava.core.httpclient.AbstractAsyncOnlyHttpClient)1 MultipartPayload (com.github.scribejava.core.httpclient.multipart.MultipartPayload)1 MultipartUtils (com.github.scribejava.core.httpclient.multipart.MultipartUtils)1 OAuth1AccessToken (com.github.scribejava.core.model.OAuth1AccessToken)1 OAuth1RequestToken (com.github.scribejava.core.model.OAuth1RequestToken)1 OAuth2AccessToken (com.github.scribejava.core.model.OAuth2AccessToken)1 OAuth20Service (com.github.scribejava.core.oauth.OAuth20Service)1 WebClient (com.linecorp.armeria.client.WebClient)1 AggregatedHttpResponse (com.linecorp.armeria.common.AggregatedHttpResponse)1