Quick and Dirty Twitter4j OAuth for Web Apps
This simple tutorial is a result of my trying to oauth to twitter using Twitter4j. It is actually part of my thesis in my university, a small functionality in a much wider application.
Anyway, I have been googling tutorials on how to use Twitter4j in a web app but I haven’t found much. The Twitter4j has examples but most of them in are written in a main method. I did find some leads in the Twitter4j google group and much to my surprise, the key to accessing data in Twitter using Twitter4j/Oauth is knowing how Oauth works in the first place.
This tutorial isn’t about how to use the oauth protocol there are many resources in the net about that .
Although this code works, it might not be the right way to use Twitter4j and Oauth. It might not be optimized and it’s definitely not the best way. It’s actually riddled with bad practices! Although in my real app, I have refactored those code smells already.
UPDATE: Here’s a refactored solution that I did.
Another thing, I have yet to address why Twitter’s icon shows up after it redirects to the to callback url. I intend to work on that issues soon as well as optimizing the method. Comments and suggestions are highly welcome!
First, Register your web app with Twitter and set there the callback url of your app. That’s the url the user is redirected to after authorization. Once you’ve registered your app, you’ll be provided with a consumer key and consumer secret that is unique to your app.
In a first servlet, You need to print the the authorization url so the user can click on it and he/she can authorize your web app to be able to access your Twitter account.
Get a request token
Twitter twitter = new Twitter(); twitter.setOAuthConsumer("consumerkey", "consumersecret"); RequestToken twitterRequestToken = twitter.getOAuthRequestToken();
Get the token and tokenSecret and then find a way to persist them.
String token = twitterRequestToken.getToken(); String tokenSecret = twitterRequestToken.getTokenSecret(); persist(token,tokenSecret);
String authorizationUrl = twitterRequestToken. getAuthorizationURL(); pageProperties.put("twitterAuthUrl",authorizationURL);
When the user sees the link and clicks on it, he/she is directed to Twitter and it asks for his/her authorization. After this the user is directed to the callback url. This is usually a second servlet but in my app it’s just another Spring abstract controller.
In a second servlet, retrieve the token and tokenSecret you persisted in the first servlet. Once that is done, get an access token
Twitter twitter = new Twitter(); twitter.setOAuthConsumer("consumerkey", "consumersecret"); AccessToken accessToken = twitter.getOAuthAccessToken(token, tokenSecret); twitter.setOAuthAccessToken(accessToken);
Again, persist the token and tokenSecret that comes with the accessToken for later use.
persist( accessToken.getToken(), accessToken.getTokenSecret() );
Once that’s out of the way, use the Twitter
instantiations many service methods to be able to access a user’s
int id = accessToken.getUserId(); User user = twitter.showUser(id+""); String screenName = user.getScreenName();
So there you go, a quick and dirty Twitter4j Oauth for web apps.