Wednesday, July 29, 2009

Integrate Twitter and bit.ly into your Java App

I am in the final stage of developing towards the 2.0 release of jRecruiter. It is the job posting service of the Atlanta Java Users Group (AJUG) and as such I devote some of my spare-time towards it. It is a great way to explore all kinds of different technologies. Thus, last weekend I had the idea that it might be a cool idea to let the system "tweet" about job posting additions and updates. Here are my findings.

First I looked at available Twitter libraries for Java:
After some considerations, I chose Twitter4J. To me, it seemed to be the most active of the three projects. Even better, it has a BSD-style license. Using Twitter4J is super simple:

Twitter twitter = new Twitter("username", "password");

try {
twitter.updateStatus("My tweet");
} catch (TwitterException e) {

}

There is so much more you can with Twitter4J. Check out its documentation for further details.

Secondly, as each tweet contains a link back to the job detail page in the main application, I was looking into ways to shorten URLs. That way, I can store more job posting related data within the only 140 characters available for each Twitter status update.

The original service for this is TinyURL. However, it looks like bit.ly has more traction these days and is also used directly by Twitter itself. The nice thing about bit.ly is that they have developer accounts and are offering a web service API for doing URL shortening.

Even better, there is already a Java library available for using bit.ly called bitlyj. Using bitlyj is actually almost as equally simple to use as Twitter4j:

final Bitly bitly =
BitlyFactory.newInstance("username", "your_bitly_api_key");
try {
BitlyUrl bUrl = bitly.shorten("http://www.google.com/");
URL url = bUrl.getShortUrl();
} catch (IOException e) {

}

Unfortunately, you have to build bitlyj yourself, but thanks to Maven it is a piece of cake. One thing I noticed, is that the error handling of the library could be more robust. Accidentally, I was using my bit.ly password, initially. However, bit.ly provides an API Key. By the way if you ever wonder where you can get that API Key-It is right in your account page. Anyway, the library needs some attention but for my simple use-case it worked fine.

2 comments:

  said...
This comment has been removed by the author.
Unknown said...

If you have a specific scenario demonstrating how error handling in bitlyj could be more robust, please file an issue at the project site. I also implemented only the parts of the API that were useful to me, but if there's a specific piece missing that you'd like, file an issue for that too. I don't give the library enough time to even call it a side project, but I do address issues when raised. Thanks for the write up!

chris

PS If you know Gabriel Claramunt of the Atlanta Scala group, tell him Chris (burningodzilla on twitter) says hello!