Android OAuth With Signpost and Retrofit

This week I finally got time to play with square’s retrofit library. The first thing I wanted to try is to use it to for Twitter API. I immediately met a problem – how to do oauth? I was hoping retrofit would just work out of the box, but I can’t find any example in the site, and even not much results when google/stackoverflow around.

It seems that twitter4j provide full support of twitter api from oauth to getting posts, but as my goal is to try out retrofit, I decided not to use twitter4j at this point. Moreover, I will call different api later so it’s better for me to figure out how to do oauth on Android.

Scribe-java

I first found this Scribe java library, I like the way the author describe the library.

Who said OAuth was difficult? Configuring scribe is so easy your grandma can do it! check it out:

And it supports many existing oauth library including Google, Facebook, Yahoo, LinkedIn, Twitter, etc… It is easy to find example on integrating scribe with android. I tried this library and can successfully get pass the authorization steps, but one problem is that the library wrapped the request object so it may not work well with retrofit, I stopped here and did not dig deeper with this library even I like this a lot.

Signpost

Then I tried Signpost (github), this is a simple library that focus on signing the request and does not wrap request with custom object. Not like Scribe, Signpost does not implement any existing service, but implementing one is just providing the corresponding oauth api url.

1
2
3
4
5
6
7
OAuthConsumer consumer = new DefaultOAuthConsumer(
        API_KEY, API_SECRET);

OAuthProvider provider = new DefaultOAuthProvider(
        "https://api.twitter.com/oauth/request_token",
        "https://api.twitter.com/oauth/access_token",
        "https://api.twitter.com/oauth/authorize");

The Android integration of signpost is similar to scribe. After the authorization steps, it can hook into retrofit by extending the client.

Here is my code for integration – gist

Reference

Comments