Created
June 29, 2013 00:24
-
-
Save julianshen/5889097 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
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
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