Authenticating to Twitter Using OAuth and twitter4j 2.2x API with ColdFusion

Twitter Logo

Ray Camden has a great blog entry on adding support in ColdFusion for Twitter but at the time he wrote it, the version of twitter4j was 2.1. Since then, twitter4j has been changed and the code in Ray's blog won't work if you need to use version 2.2.x. Specifically, the Twitter object is now an interface and the can't be directly instantiated. Also, the setOAuthAccessToken method on the Twitter interface no longer accepts the access token and access token secret as paremeters. The method now accepts an AccessToken object.

The change in code is small, but important.  To make the new version of the API work, change the following three lines from Ray's blog to these new method calls and everything should work as he explains.

(Sorry about the wrapped lines, but you get the idea)


<cfset application.Twitter = application.javaloader.create("twitter4j.Twitter")> <cfset application.Twitter.setOAuthConsumer ('consumerkey',consumersecret')> <cfset application.Twitter.setOAuthAccessToken ("storedaccesstoken" ,"storedaccesssecret")>


<cfset application.Twitter = javaloader.create ("twitter4j.TwitterFactory").getInstance()> <cfset application.Twitter.setOAuthConsumer ('consumerkey',consumersecret')> <cfset local.accessToken = javaloader.create ("twitter4j.auth.AccessToken").init ("storedaccesstoken" ,"storedaccesssecret")> <cfset application.Twitter.setOAuthAccessToken (local.accessToken)>

Since there is no ColdFusion Twitter client out there, I plan on building one soon that you can just drop into your applications. Stay tuned for that blog entry where I'll walk you through using it.