Friday, December 31, 2010

Android Oauth.

We all know OAUTH authentication mechanism is secure and works great. To understand OAUTH more see wiki article and OAuth official website. Implementing OAUTH for Android was tricky since a user has to login and authenticate using the browser, which will redirect the user back to our Android application. Once an android application/intent goes to the background and resumes, all the locally stored Objects are reset. So this will cause an Exception/error and break out of the application when using OAUTH, we will see how to avoid this.

For using OAuth in android we will use a library called oauth-signpost written in java. Download the oauth-signpost core and commons http jars.

Assuming that you know the basics of android like editing manifest files, configuring build paths etc I will be more specific about the problem and the tricks to avoid/resolve them. One of the options I read online(stack overflow) to overcome Objects getting reset was " First of all, you do not need to save the whole consumer and provider object. All you need to do is store the requestToken and the requestSecret. Luckily, those are Strings, so you don't need to write them to disk or anything. Just store them in the sharedPreferences or something like that." Yes you can use sharedPreferences but there is a more simpler way I used, just declare all the String objects as Static. They wont be reset and it worked for me like charm!

I will also give a step by step tutorial about how to use OAUTH for android. We will use Twitter Oauth. I will post the code soon..

No comments: