The idea behind this post is to illustrate how to integrate Twitter with an Android app. I have also included a demo where OAuth is used to authenticate the user and send tweets from the Android app.
OAuth is open standard used to authenticate and share user resources and information of a site to a third party application or site. This is a secure method of integrating popular social media sites with an application. More can be read here http://en.wikipedia.org/wiki/OAuth .
This demo project contains two parts:
- OAuth authentication.
- Sending tweet.
But before building the application let me talk about Twitter developer account.
Twitter Developer account and creating an application:
To integrate Twitter in an application, developers need to have a Twitter account. If you already have one, you can use it to log into https://dev.twitter.com/ . The email address mentioned in your twitter account will be used to make twitter objects in the code. Hence it is advised to have separate developer email address and twitter account.
Once logged into the Twitter developer site, you can create a new application from “My Applications” menu selection. You will have to fill a simple form with application name, description of the application, application website, and Callback URL (enter some URL in the “Callback URL” field. DO NOT leave it empty). Once your application is created, click on the settings tab of the application description and select “Read and Write” in “Access” from the “Application Type” section (shown in pic below). This will enable sending tweets from your applications.
NB: These are basic settings. You may also choose to upload icon, mention organization name/website, change OAuth setting of your application.
Building an application:
Create an Android application in your Eclipse. Select the Android OS you want to run it in (Android 2.2 is recommended as it has, currently, second largest distribution and is easily compatible with newer versions like 2.3.x, which has the largest distribution).
Create a folder called “lib” and copy jtwitter.jar (Twitter api) , signpost-core-188.8.131.52.jar (OAuth signpost api) and signpost-commonshttp4-1.2.jar. Then include these jars in the buildpath. (These jars are present in the project source shared).
Add the <uses-permission> tag for your application to use internet.
Then add the <intent-filter> tag for the OAuth callback.
This intent filter takes care of launching the browser for OAuth and redirecting back to your application after the authentication process is complete.
Create a simple layout with one textbox , and two button (tweet button and logout button) (as seen in pic below)
The consumer key and consumer secret key generated for your application created in dev.twitter.com should be used here. Also use your email id registered with twitter (replace androidtestacc1 <at> gmail <dot> com with your email id). These are the constants that you will need later.
Create OauthConsumer and OauthProvider objects for obtaining and setting Oauth tokens for the user.
NB: requestTokenEndpointUrl (http://api.twitter.com/oauth/request_token) , accessTokenEndpointUrl (http://api.twitter.com/oauth/access_token) , authorizationWebsiteUrl ( http://api.twitter.com/oauth/authorize) used in the creation of the OauthProvider object used “https” before. Now “http” is used. Hence it’s a good idea to keep track of what changes are employed by Twitter OAuth by refering to the official Twitter Oauth doc https://dev.twitter.com/docs/auth (remember to refer to this when your app mysteriously stops working).
Retriving OAuth request token:
Create an AsyncTask class to retrieve the request token in case the app is not authorized by OAuth. In this demo project, as soon as the app is launched, it checks if it is authorized by Twitter OAuth for the user. If not, it starts the Oauth authenticattion by launching browser. Below is the code where OauthProvier object(viz. mProvider) is used to retrieve the request token. The “ retrieveRequestToken” method queries the service provider for a request token. OauthConsumer object (viz. mConsumer) object is user to sign the request for the access token. And the Call back URL (viz. CALLBACK_URL) is where the app is directed after the complete authorization process (in our case it is the same activity which initiates the OAuth process, refer to the <data> tag in the <intent-filter> element in the above AndroidManifest.xml).
Create an AsyncTask class to retrieve the access token from theOAuth verifier code. This class must be called when the Call Back URL is passed as a parameter in the “retrieveRequestToken “ method. Call this class by overriding the method “onNewIntent” (refer to the code for implementation). After the OAuth access token and secret is retrieved, store them in the shared preferences.
NB: Twitter does not expire the access token. So in case you want to clear the session(eg: logout. Once the access token is cleared the user will have to authorize the app again), the developer will have to write code to explicitly clear the access token and request for new access token and access token secret in the next login.
Source: https://dev.twitter.com/docs/auth/oauth/faq (How long does an access token last?)
Once OAuth process is done, you can send tweets by merely using the Twitter object. Get the string entered by the user in the EditText box and pass it to “setStatus” of Twitter object. Again use an AsyncTask class to post the tweet in the background.
This will successfully post the tweet.
The code for the above demo can be found here : https://github.com/punitmg/Twitter-Test-App