Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
package com.htc.blinkfeed.sample.vimeo.api;
import java.io.IOException;
import java.net.HttpURLConnection;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.http.HttpRequest;
import retrofit.client.OkClient;
import retrofit.client.Request;
public class SignedOkClient extends OkClient {
OAuthConsumer mConsumer =null;
public SignedOkClient(OAuthConsumer consumer) {
super();
mConsumer = consumer;
}
@Override
protected HttpURLConnection openConnection(Request request)
throws IOException {
HttpURLConnection connection = super.openConnection(request);
try {
HttpRequest signedReq = mConsumer.sign(connection);
} catch (OAuthMessageSignerException e) {
//Fail to sign, ignore
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
//Fail to sign, ignore
e.printStackTrace();
} catch (OAuthCommunicationException e) {
//Fail to sign, ignore
e.printStackTrace();
}
return connection;
}
}

I didn't get this to work, since the request object is immutable and the client won't operate on the request that is returned by the sign method. Instead I ended up overriding the execute method. The end result is part of an extension to oauth-signpost, found here: https://github.com/pakerfeldt/signpost-retrofit

aitbaali commented Jan 10, 2017 edited

Try:

import retrofit.client.UrlConnectionClient;

class MConnectionClient extends UrlConnectionClient {

        @Override
        protected HttpURLConnection openConnection(Request request) throws IOException {
            HttpURLConnection connection = super.openConnection(request);

            try {
                OAuthConsumer oAuthConsumer = new DefaultOAuthConsumer(CLIENT_ID, CLIENT_SECRET); //oAuth Keys
                oAuthConsumer.setTokenWithSecret(token, tokenSecret);
                oAuthConsumer.sign(connection);
            } catch (OAuthMessageSignerException e) {
                e.printStackTrace();
            } catch (OAuthExpectationFailedException e) {
                e.printStackTrace();
            } catch (OAuthCommunicationException e) {
                e.printStackTrace();
            }
            return connection;
        }
    }
RestAdapter restAdapter = new RestAdapter.Builder()
                 ......
                .setClient(new MConnectionClient())
                .build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment