Twitter a microblogging service is getting more and more popular these days. A lot of developers are involved to develop applications on its API.
Two weeks ago I click with a very basic idea and being as a matter of learning I started working on it. I have finished more than 80% of the work. I deployed the application on Google App Engine (I hope you are familiar with it). I used Twitter4J lib a Java wrapper for Twitter API.
If you are also interested in Twitter based app development in Jave, then this tutorial will be helpful for you. Feel free to add comments at the end of the post, I will love to reply.
Things you require for development
Things you need to know before you start
Oauth authentication
You should know Oauth basics and its terminologies. You can little google on it or read this FAQs
Register an App with Twitter
You need to register an application on this URL: http://twitter.com/oauth. Please take care of two things. The call back URL will not be your localhost URL. It should be a valid web address. And while choosing Default Access Type, if your application need to do changes or send tweets then you should choose Read & Write otherwise/if you just want to do readonly operations then leave Read-only checked.
A little bit about Google App Engine
Google App Engine is cloud based hosting environment. You should read on their web or on a post by me.
Steps for any Twitter App
- User is on your website
- Generate a token
- Have a hyperlink and take user to the Twitter from your website for authentication
- User will enter its user name and passeword and press allow
If user’s credentials authentiecated Twitter will call the callback method which you had mentioned. Note that localhost URLs will not work here. You need mentiona a valid web address which will be invoked when user will be authenticated.
Code Section
I will assume that you have developed a helloworld prject in Google App Engine and deployed it on appspot.com domain. Code snippet is available in 2 servlets and 1 jsp page.
LoginServlet.java
Twitter twitter = new Twitter();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
Constants.CONSUMER_SECRET);
RequestToken requestToken = twitter.getOAuthRequestToken();
String token = requestToken.getToken();
String tokenSecret = requestToken.getTokenSecret();
HttpSession session = request.getSession();
session.setAttribute("token", token);
session.setAttribute("tokenSecret", tokenSecret);
String authUrl = requestToken.getAuthorizationURL();
request.setAttribute("authUrl", authUrl);
RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
rd.forward(request, response);
Consumer key and secrets will be generated when you register an application with Twitter. You need to keep token information into session so that you can use the token when callback URL will be called. authUrl is a link which will take user to the Twitter website for authentication. And if authentication successful it will call your URL mentioned as callback.
login.jsp
<a href='<%=request.getAttribute("authUrl") %>'>Sign in with Twitter</a>
HomeServlet.java (as callback URL)
Twitter twitter = new Twitter();
HttpSession session = request.getSession();
twitter.setOAuthConsumer(Constants.CONSUMER_KEY,
Constants.CONSUMER_SECRET);
AccessToken accessToken = twitter.getOAuthAccessToken(
(String) session.getAttribute("token"), (String) session
.getAttribute("tokenSecret"));
twitter.setOAuthAccessToken(accessToken);
User user = twitter.verifyCredentials();
HomeServlet is your callback. Let say you have mentioned URL mapping of this servlet as /Home. So you mention http://.appspot.com/Home in callback field in your app registeration page at twitter. And this HomeServlet will be called. Now you have the user object to play with. See Twitter4J javadocs for more help.
WEB-INF/appengine-web.xml
<sessions-enabled>true</sessions-enabled>
You need to add this tag in you appengine-web.xml file that you are enabling the session.
So this was a tutorial, feel free to ping me on it. If you stuck somewhere. We will both look into it.
I also suggest following links to you people to must visit them. They helps me a lot in the understanding and the development. I will update this tutorial if got more things to discuss.
Helpful Links
- How to Register an App with Twitter for OAuth
- Writing Your First Twitter Application with OAuth
- Quick and Dirty Twitter4j OAuth for Web Apps
- Finding a Web Solution for Twitter4j: Results of Session Solution
Pingback: How to use and retain Twitter4J OAuth access token
Pingback: oAuth « Identity Management , SOA, Testing, Monitoring
Pingback: twitter4j | cg2p