Search in sources :

Example 1 with OAuthCommunicationException

use of oauth.signpost.exception.OAuthCommunicationException in project data-transfer-project by google.

the class SmugMugPhotoService method postRequest.

private <T> T postRequest(String url, HttpContent content, Map<String, String> headers, TypeReference<T> typeReference) throws IOException {
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    String fullUrl = url;
    if (!fullUrl.contains("://")) {
        fullUrl = BASE_URL + url;
    }
    HttpRequest postRequest = requestFactory.buildPostRequest(new GenericUrl(fullUrl), content);
    HttpHeaders httpHeaders = new HttpHeaders().setAccept("application/json").setContentType("application/json");
    for (Entry<String, String> entry : headers.entrySet()) {
        httpHeaders.put(entry.getKey(), entry.getValue());
    }
    postRequest.setHeaders(httpHeaders);
    try {
        postRequest = (HttpRequest) this.authConsumer.sign(postRequest).unwrap();
    } catch (OAuthMessageSignerException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't create post request", e);
    }
    HttpResponse response;
    try {
        response = postRequest.execute();
    } catch (HttpResponseException e) {
        throw new IOException("Problem making request: " + postRequest.getUrl(), e);
    }
    int statusCode = response.getStatusCode();
    if (statusCode < 200 || statusCode >= 300) {
        throw new IOException("Bad status code: " + statusCode + " error: " + response.getStatusMessage());
    }
    String result = CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8));
    return MAPPER.readValue(result, typeReference);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) InputStreamReader(java.io.InputStreamReader) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) HttpResponse(com.google.api.client.http.HttpResponse) HttpResponseException(com.google.api.client.http.HttpResponseException) GenericUrl(com.google.api.client.http.GenericUrl) IOException(java.io.IOException) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Example 2 with OAuthCommunicationException

use of oauth.signpost.exception.OAuthCommunicationException in project data-transfer-project by google.

the class SmugMugPhotoService method makeRequest.

private <T> SmugMugResponse<T> makeRequest(String url, TypeReference<SmugMugResponse<T>> typeReference) throws IOException {
    HttpRequestFactory requestFactory = httpTransport.createRequestFactory();
    String signedRequest;
    try {
        signedRequest = this.authConsumer.sign(BASE_URL + url + "?_accept=application%2Fjson");
    } catch (OAuthMessageSignerException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't get albums", e);
    }
    HttpRequest getRequest = requestFactory.buildGetRequest(new GenericUrl(signedRequest));
    HttpResponse response = getRequest.execute();
    int statusCode = response.getStatusCode();
    if (statusCode != 200) {
        throw new IOException("Bad status code: " + statusCode + " error: " + response.getStatusMessage());
    }
    String result = CharStreams.toString(new InputStreamReader(response.getContent(), Charsets.UTF_8));
    return MAPPER.readValue(result, typeReference);
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) InputStreamReader(java.io.InputStreamReader) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) HttpResponse(com.google.api.client.http.HttpResponse) IOException(java.io.IOException) GenericUrl(com.google.api.client.http.GenericUrl) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Example 3 with OAuthCommunicationException

use of oauth.signpost.exception.OAuthCommunicationException in project twitterdroid by fbrunel.

the class ConfigActivity method onCreate.

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    try {
        String authURL = provider.retrieveRequestToken(consumer, CALLBACK_URL);
        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authURL)));
    } catch (OAuthMessageSignerException e) {
        e.printStackTrace();
    } catch (OAuthNotAuthorizedException e) {
        e.printStackTrace();
    } catch (OAuthExpectationFailedException e) {
        e.printStackTrace();
    } catch (OAuthCommunicationException e) {
        e.printStackTrace();
    }
    finish();
}
Also used : OAuthNotAuthorizedException(oauth.signpost.exception.OAuthNotAuthorizedException) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) Intent(android.content.Intent) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Example 4 with OAuthCommunicationException

use of oauth.signpost.exception.OAuthCommunicationException in project data-transfer-project by google.

the class SmugMugAuth method generateAuthData.

@Override
public AuthData generateAuthData(IOInterface ioInterface) throws IOException {
    // As per details: https://api.smugmug.com/api/v2/doc/tutorial/authorization.html
    // and example:
    // http://stackoverflow.com/questions/15194182/examples-for-oauth1-using-google-api-java-oauth
    // Google library puts signature in header and not in request, see https://oauth.net/1/
    OAuthConsumer consumer = new GoogleOAuthConsumer(appCredentials.key(), appCredentials.secret());
    String permissions = (serviceMode == ServiceMode.EXPORT) ? "Read" : "Add";
    OAuthProvider provider = new DefaultOAuthProvider("https://secure.smugmug.com/services/oauth/1.0a/getRequestToken", "https://secure.smugmug.com/services/oauth/1.0a/getAccessToken", "https://secure.smugmug.com/services/oauth/1.0a/authorize?Access=Full&Permissions=" + permissions);
    String authUrl;
    try {
        authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND);
    } catch (OAuthMessageSignerException | OAuthNotAuthorizedException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't generate authUrl", e);
    }
    String code = ioInterface.ask("Please visit: " + authUrl + " and enter code:");
    try {
        provider.retrieveAccessToken(consumer, code.trim());
    } catch (OAuthMessageSignerException | OAuthNotAuthorizedException | OAuthExpectationFailedException | OAuthCommunicationException e) {
        throw new IOException("Couldn't authorize", e);
    }
    return TokenSecretAuthData.create(consumer.getToken(), consumer.getTokenSecret());
}
Also used : OAuthNotAuthorizedException(oauth.signpost.exception.OAuthNotAuthorizedException) GoogleOAuthConsumer(org.dataportabilityproject.shared.signpost.GoogleOAuthConsumer) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) OAuthProvider(oauth.signpost.OAuthProvider) DefaultOAuthProvider(oauth.signpost.basic.DefaultOAuthProvider) OAuthConsumer(oauth.signpost.OAuthConsumer) GoogleOAuthConsumer(org.dataportabilityproject.shared.signpost.GoogleOAuthConsumer) IOException(java.io.IOException) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Example 5 with OAuthCommunicationException

use of oauth.signpost.exception.OAuthCommunicationException in project twitterdroid by fbrunel.

the class TwitterConnection method makeAuthConnection.

private HttpURLConnection makeAuthConnection(String resource) throws IOException {
    HttpURLConnection conn = makeConnection(resource);
    conn.setUseCaches(false);
    try {
        consumer.sign(conn);
    } catch (OAuthMessageSignerException e) {
        e.printStackTrace();
    } catch (OAuthExpectationFailedException e) {
        e.printStackTrace();
    } catch (OAuthCommunicationException e) {
        e.printStackTrace();
    }
    return conn;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) OAuthMessageSignerException(oauth.signpost.exception.OAuthMessageSignerException) OAuthExpectationFailedException(oauth.signpost.exception.OAuthExpectationFailedException) OAuthCommunicationException(oauth.signpost.exception.OAuthCommunicationException)

Aggregations

OAuthCommunicationException (oauth.signpost.exception.OAuthCommunicationException)5 OAuthExpectationFailedException (oauth.signpost.exception.OAuthExpectationFailedException)5 OAuthMessageSignerException (oauth.signpost.exception.OAuthMessageSignerException)5 IOException (java.io.IOException)3 GenericUrl (com.google.api.client.http.GenericUrl)2 HttpRequest (com.google.api.client.http.HttpRequest)2 HttpRequestFactory (com.google.api.client.http.HttpRequestFactory)2 HttpResponse (com.google.api.client.http.HttpResponse)2 InputStreamReader (java.io.InputStreamReader)2 OAuthNotAuthorizedException (oauth.signpost.exception.OAuthNotAuthorizedException)2 Intent (android.content.Intent)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpResponseException (com.google.api.client.http.HttpResponseException)1 HttpURLConnection (java.net.HttpURLConnection)1 OAuthConsumer (oauth.signpost.OAuthConsumer)1 OAuthProvider (oauth.signpost.OAuthProvider)1 DefaultOAuthProvider (oauth.signpost.basic.DefaultOAuthProvider)1 GoogleOAuthConsumer (org.dataportabilityproject.shared.signpost.GoogleOAuthConsumer)1