How To Use Twitter OAuth On Android
If you are developing an application for the Android platform, and you need to interact with the Twitter API, you now have to use OAuth to authenticate the user. In this article, we will have a look on how you can do that.
What is OAuth?
OAuth is a way of accessing a user’s data (e.g. tweets) without asking for the user’s username and password. Your application opens the Twitter website which will ask the user if they want to allow you to access their data. If they do, they are taken back to the application and can start using it. You can find more about OAuth all over the web.
Prerequisites
There are a couple of .jars that you will need for this to work.
- signpost-commonshttp4-1.2.1.1.jar
- signpost-core-1.2.1.1.jar
You can download them here.
Basic Activity
Let’s say we have an activity running where the user can start the authentication process. There is nothing special about this activity, except for some text and a button. When the user clicks the button, the OAuth process will be started. From the button’s onClickListener() we will call the startOAuth() method of our activity.
We will add a few attributes to our activity. Let’s call the activity Main.
consumerKey and consumerSecret will store your app’s unique keys that you will get from Twitter. CALLBACK is a little different. This is used when the application is authorized on the web, and the control is returned back to the Main activity. For the mobile browser to be able to call the application and tell it that the OAuth business has gone well, it needs a call back. Both app and twitter can be exchanged for anything else.
Now let’s have a look at the startOAuth() method.
Here we create the necessary OAuth objects which will in turn generate the unique authenticating URL. Once we have the URL we open the browser and point it to that URL. The user will be presented with a dialog asking them to allow or to deny your application access.
In order for our activity to be able to receive the callback, we need to add a few things the Android manifest file. Change the applications definition to the following:
Note that if you changed the app and twitter in the CALLBACK variable above, you will need to make sure that the change is reflected here. This basically allows the activity to receive data from a foreign source - our browser.
Now we need to catch the callback and handle it. We do that by overriding the onNewIntent() method of our Main activity.
OK, there’s quite a bit there. We extract the data that the browser sent back to us. This data is used to verify that the authentication was successful and that we can now access the user’s data. From the data, we get the user’s key and their secret. We save that into the application’s shared preferences file and return.
Now we are good to go. We can make authenticated requests to TwitterAPI on behalf of the user.
For example, to get the user’s home timeline, you would do something like:
And the array variable is a list of the latest tweets in the user’s home timeline.
Thanks
akm
www.cdacians.com
No comments:
Post a Comment