sábado, 1 de noviembre de 2008

Twitter Access in Java

After a long time I am back in the blogging world. Because I hope French and Mexican people will read this blog I have to write in English...

Since some days ago I am using twitter again (my id is yannart) but I have a big problem. From work I can't access to the main domain twitter.com. I try a personal proxy I have installed in a (secret) Internet URL and it works with some bugs. After that I start to wonder how difficult will be coding my own web app with server-side communication against twitter.

Java is my favorite programming language so I search if there where some APIs making easy the communication between a Java program and the twitter servers. I found 3:
But which one is better?
It's a difficult question I can't respond. However I chose Twitter4J for some simple reasons:
  • Good documentation
  • Asynchronous support

Let's code a simple application!

Usually I use Eclipse or NetBeans IDEs but I this example I will use only Maven 2 so everybody can adapt it in his favorite IDEs (using the official plug-in in NetBeans o the m2eclipse plug-in in Eclipse).
So, that's what you need:

Creation of the project with Maven

In the console run this:

mvn archetype:create \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DgroupId=org.yannart.twitter \

Maven will create the project in a folder named ConsoleTwitter.

Adding the dependencies

Open the folder of the project and edit the pom.xml file:

Add the Twitter4J dependency between the <dependencies> and </dependencies> tags:
We need to configure the support for Java 5 because the default in Maven is Java 1.4 (for now):
Add this code between the <project> and </project> tags:

Modify the main class to retrieve data from twitter

Go to the folder src/main/java/org/yannart/twitter/ and open the file App.java.

Replace the existing code in the main method with that one:

public static void main( String[] args ){

if(args.length != 2){
System.out.println("ERROR - you must specify a twitter username and a password.");
System.out.println("Usage: [username] [password]");

try {
Twitter twitter = new Twitter(args[0], args[1]);

twitter.update("Updating Twitter from my own Java code!");

List statuses = twitter.getUserTimeline();

System.out.println(" -- " + args[0].toUpperCase() + " timeline --");

//prints each status of the public user timeline
for (Status status : statuses) {
System.out.println(" - " + status.getText());

} catch (TwitterException ex) {
This method will connect to Twitter using your username/password and will send the message "Updating Twitter from my own Java code!" and print your user timeline.

Compile the project

In the console go to the ConsoleTwitter folder and execute the command:

mvn install

This will compile your project and create a target folder with the binaries.

Run the project

To simplify the execution of the example, we use the exec Maven plug-in. Just run the command:
mvn exec:java -Dexec.mainClass="org.yannart.twitter.App" -Dexec.args="USERNAME PASSWORD"

Remplacing USERNAME and PASSWORD with yours.

Downloading the sources

This project is ready to execute. Just download it, compile it and run it.