Posting to Twitter from Coldfusion using Twitter4j

Published: {ts '2011-02-12 00:00:00'}
Author: Steven Neiland
Site Url: http://www.neiland.net/article/posting-to-twitter-from-coldfusion-using-twitter4j/

As a follow on to my post about using bit.ly to shorten urls for submitting to twitter, today I want to talk about how to automatically submit a tweet from coldfusion to the twitter api using twitter4j. Before I go into how to post a tweet from coldfusion however I want to cover how Twitter authenticates status updates.

Twitter and Oauth

Before June/July of 2010 posting a status update to twitter was a simple matter of sending a username, password and tweet to the twitter api url. This method used the Basic Http Authentication system and was both simple and insecure.

After June/July Twitter switched over to using a method of authentication called OAuth. The OAuth method of authenticating a user/application work on the principle of shared and secret tokens being used to sign a specific request to the twitter api.

Advantages of OAuth

This method of authentication gives two main advantages. Firstly it is more secure and you never transmit your username or password when posting an update. Secondly it allows for users to authorise third party applications to send/recieve data from your twitter account without having to give them your password.

Disadvantages of OAuth

While very powerful, OAuth as implemented by twitter has two major downsides, which are essentially two sides of the same coin.

Firstly it is far more complicated to implement. A typical work flow of authenticating an application requires numerious detailed steps to complete.

Secondly as a result of the complexity of the signing method it is very difficult to diagnose problems. This is also not helped by the documentation which while thorough looks to be written by someone intimately familiar with OAuth for somebody who is intimately familiar with OAuth. Further there are no good tools available for developers (that I have found) that can help diagnose / demonstrate what the expected outcome of a particular step in the authentication process should be.

Implementing in ColdFusion

Currently there are no simple Coldfusion implementations for talking to twitter using OAuth. I have found a few libraries but they are considerably bloated for the simple process of posting a tweet. For this reason I have started developing my own simple library which I will cover at a later date.

In the mean time I want to show how I recently was able to post to twitter from Coldfusion using a java library.

Posting to Twitter using Twitter4j

Twitter4J is an unofficial Java library for the Twitter API which allows you to easily integrate your Java/Coldfusion application with the Twitter service. Installing and using is a simple matter of following the following steps.

Step 1: Create a Twitter Application

In order to post to the twitter api you must first create an application twitter. Simply goto the http://dev.twitter.com/ site and login using your twitter details. Once logged in you will be presented with a three options 'Explore', 'Register an Application' and 'Discuss'.

Select to register a new app and fill out the form. Make sure to select the application type as browser with "read & write" access.

Once you have created your application you need to take note of the following values.

Step 2: Download twitter4j Jar File

Downloading is a simple matter of going to the http://twitter4j.org/ website and downloading the latest snapshot zip file. The current one at time of posting is '2.1.9'. Do not download the in development version.

Step 3: Install twitter4j in Coldfusion

To install twitter4j first unzip the download. Then navigate to the '[extracted folder]/lib/' directory and locate the 'twitter4j-core-[version number]-SNAPSHOT.jar' jar file.

Copy this file to "{coldfusion directory}/bin/lib" and name twitter4j.jar. Finally restart coldfusion.

Posting to Twitter using Twitter4j

At this stage posting to twitter is a simple matter of create a java object instance of twitter4j, setting the consumer and access values and running the updateStatus() command.

So there you have it, posting to twitter from coldfusion.

Update: New Api

Since writing the article twitter updated their api and the twitter4j library was subsequently updated accordingly. The new code for using twitter4j v3.0.x can be found here.