Using Twitter oauth on GAE/J

Twitter supports oauth and there are many libraries for it, for  Java(twitter4J)(oauth-signpost),  most of the time you see examples of a desktop version, so it can take time to implement it on a web application (it did take time for my case).

Step 1. Get a Twitter account and create a Twitter application from http://twitter.com/apps (don’t forget to check in browser and enter a  valid callback url. Callback URL can not be localhost :)

Step 2.  Download twitter4J add the library (in my case twitter4j-core-2.1.1-SNAPSHOT.jar) to project and update classpath

Step 3. Create a web application (I assume you have the Google plugin for Eclipse )

Step 4. Add the following servlets and update your web.xml. First servlet will handle login part. It will create a RequestToken which will create the magical aggrements between the consumer(your application) and provider(twitter). First servlet save the  token and secret token since we will need them later.

String token = requestToken.getToken();
String tokenSecret = requestToken.getTokenSecret();

Step 5. You forget to update web.xml didn’t you, update the web.xml

Step 6.  Enable session by adding

    <sessions-enabled>true</sessions-enabled>

to appengine-web.xml.

You can see my version from http://90.latest.denemekaya.appspot.com/login

ps. don’t forget to set the callback in Twittter to the url of the servlet for callback.

Links:

  • http://www.pakzilla.com/2009/10/03/tutorial-java-based-twitter-app-on-google-app-engine/
  • Explain the same procedure with Grails — http://blogs.bytecode.com.au/glen/2009/12/08/log-into-your-grails-app-using-your-twitter-credentials.html

Post a Comment

Your email is never shared. Required fields are marked *

*
*