Author: Steven Neiland
Published:

Warning: This blog entry was written two or more years ago. Therefore, it may contain broken links, out-dated or misleading content, or information that is just plain wrong. Please read on with caution.

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

register_app_01

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.

  • Consumer Key
  • Consumer Secret
  • Access Token
  • Access Token Secret

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.

<!--- Set our values from twitter--->
<cfset consumerKey = "{your consumer key}">
<cfset consumerSecret = "{your consumer secret value}">
<cfset accessToken = "{your access token value}">
<cfset accessTokenSecret = "{your access token secret value}">

<!---Create java instance of twitter4j and set key values--->
<cfset Twitter = createObject("Java", "twitter4j.Twitter") />
<cfset Twitter.setOAuthConsumer(consumerKey,consumerSecret)>
<cfset Twitter.setOAuthAccessToken(accessToken ,accessTokenSecret)>

<!--- Post a simple message to twitter--->
<cfset twitter.updateStatus("Hello world tweet tweet")>

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.

Related Blog Postings

What Do You Think?

Reader Comments

Mario Conforti's Gravatar
Mario Conforti
Monday, January 23, 2012 at 9:29:32 AM EST

Hi, thanks for the script, but as you can see at the URL I am getting this error:
The setOAuthConsumer method was not found.
Either there are no methods with the specified method name and argument types or the setOAuthConsumer method is overloaded with argument types that ColdFusion cannot decipher reliably. ColdFusion found 0 methods that match the provided arguments. If this is a Java object and you verified that the method exists, use the javacast function to reduce ambiguity.
I used the latest version 2.2.6
Do you think this might be the problem?
Thanks
mc.

Mario Conforti's Gravatar
Mario Conforti
Monday, January 23, 2012 at 10:14:33 AM EST

The problem IS the version. I found an older twitter4j jar, and now evarything is fine.Thanks,
mc.

Steven Neiland's Gravatar
Steven Neiland
Monday, January 23, 2012 at 10:16:36 AM EST

Glad you were able to figure it out mario.

Talal M's Gravatar
Talal M
Monday, October 8, 2012 at 3:53:38 AM EDT

I get the following error:
401:Authentication credentials were missing or incorrect.
{"error":"Read-only application cannot POST","request":"\/1\/statuses\/update.json"}
So I went to set the settings of my twitter app to read and write permision but I still get it.
Any idea would really be appreciated
Thank you in advance
Talal M.

Steven Neiland's Gravatar
Steven Neiland
Monday, October 8, 2012 at 9:24:27 AM EDT

First try to reset your API keys if you changed permission settings.

Then reenter the new values into twitter4j.

Also make sure your server time is accurate as I've seen a slip of 10 minutes cause authentication issues.

Talal M's Gravatar
Talal M
Thursday, October 11, 2012 at 9:43:48 AM EDT

Thank you Steve, I followed your advice and it worked fine!
Also, I may add something, I was testing on my localhost machine and when moved to the actual server it worked and I wonder if it was a part of the permission problem.
All the Best Steve

Steven Neiland's Gravatar
Steven Neiland
Thursday, October 11, 2012 at 12:23:26 PM EDT

@Talal,
Yes the machine you were testing on would be an issue as I believe the domain/ipaddress of the calling application is part of the oauth validation.

You'd have to confirm that as I don't use oAuth more than I have to, so I'm far from being an expert.

Post a Comment

Comment Etiquette:

  • Please keep comments on-topic.
  • Please do not post unrelated questions or large chunks of code.
  • Please do not engage in flaming/abusive behaviour.
  • Comments that contain or appear to be advertisments, will not be published.
  • Comments that appear to be created for the purpose of linkbuilding to commercial sites will be removed.

We are all adults here so play nice.

*
*



Archives Blog Listing

Tag Listing

Learn CF In A Week

Treehouse

 
Fork me on GitHub