Skip to content

Instantly share code, notes, and snippets.

@julianshen
Created June 29, 2013 00:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianshen/5889097 to your computer and use it in GitHub Desktop.
Save julianshen/5889097 to your computer and use it in GitHub Desktop.
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;
}
}
@pakerfeldt
Copy link

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
Copy link

aitbaali commented Jan 10, 2017

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