Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Easy way to use twitter4j with ColdFusion
<cfcomponent output="false">
<cffunction name="init" returntype="Twitter">
<cfargument name="consumerKey" required="true">
<cfargument name="consumerSecret" required="true">
<cfargument name="authKey" type="string" required="false" default="">
<cfargument name="authSecret" type="string" required="false" default="">
var local = {};
variables.TwitterConsumerKey = arguments.consumerKey;
variables.TwitterConsumerSecret = arguments.consumerSecret;
variables.authKey = arguments.authKey;
variables.authSecret = arguments.authSecret;
local.accessToken = createObject("java", "twitter4j.auth.AccessToken").init(
variables.Twitter = createObject("java", "twitter4j.TwitterFactory").getInstance(
local.accessToken );
if( len(variables.authKey) gt 0 && len(variables.authSecret) gt 0 ){
return this;
<cffunction name="getAuthorizationURL">
<cfargument name="callbackUrl" required="false">
<cfset var localCallbackUrl = 'yourCallbackUrl'>
<cfif isDefined("arguments.callbackUrl")>
<cfset localCallbackUrl = arguments.callbackUrl>
<cfset variables.RequestToken = variables.Twitter.getOAuthRequestToken(localCallbackUrl)>
<cfset saveRequestToken(variables.RequestToken )>
<cfreturn variables.RequestToken.getAuthorizationURL()>
<cffunction name="saveRequestToken">
<cfargument name="RequestToken" required="yes">
<!--- This needs to be saved somewhere. --->
<cfset client.oAuthRequestToken = arguments.RequestToken.getToken() />
<cfset client.oAuthRequestTokenSecret = arguments.RequestToken.getTokenSecret() />
<cffunction name="saveAuthAccessToken">
<cfargument name="oAuthVerifier" required="yes">
var accessToken = variables.Twitter.getOAuthAccessToken(
You'll want to save these 2 values somewhere persistent
<cfreturn accessToken>
<cffunction name="getAuthAccessToken">
<cfreturn variables.Twitter.getOAuthAccessToken()>
<cffunction name="updateStatus">
<cfargument name="newStatus" required="true">
<cfset variables.Twitter.updateStatus("#arguments.newStatus#")>
<cffunction name="onMissingMethod">
<cfargument name="MissingMethodName" type="string" required="true"
hint="The name of the missing method." />
<cfargument name="MissingMethodArguments" type="struct" required="true"
hint="The arguments that were passed to the missing method. This might be a named argument set or a numerically indexed set."/>
<cfreturn evaluate("variables.Twitter.#missingMethodName#()")>
<cfcomponent output="false">
<cffunction name="init">
<cfargument name="Twitter" type="Twitter">
<cfset variables.Twitter = arguments.Twitter />
<cfset variables.Bitly = createObject('component', 'pathToBitly').init(
username='BITLY_USERNAME', apikey='BITLY_API_KEY', parse=true ) />
<cfreturn this>
<cffunction name="sendTweet">
<cfargument name="message">
<cfargument name="link">
<cfargument name="hashtags" type="array">
var local = {};
// Keep track of message length
local.msgLengthRemaining = 140;
// Shorten link
local.shortLink = shortenLink(link);
local.msgLengthRemaining -= Len(local.shortLink) + 1; // plus 1 is for separating space
// Make our string of hashtags
local.hashTagList = "";
for( local.i = 1; local.i lte arrayLen(arguments.hashTags); local.i++ ){
local.tmpHashTag = arguments.hashTags[i];
if( left(local.tmpHashTag,1) eq '##' ){
local.hashTagList &= arguments.hashTags[i] & " ";
local.hashTagList &= "##" & arguments.hashTags[i] & " ";
local.hashTagList = Trim(local.hashTagList);
local.msgLengthRemaining -= Len(local.hashTagList) + 1; // plus 1 is for separating space
// Truncate message (if necessary)
local.tweet = left(HTMLEditFormat(arguments.message), local.msgLengthRemaining - 1);
// this should conditionally add these things.
// it'll waste 2 chars if shortlink and hashtaglist are empt strs
variables.twitter.updateStatus("#local.tweet# #local.shortLink# #local.hashTagList#");
<cffunction name="shortenLink">
<cfargument name="link">
var local = {};
local.shortLink = '';
local.bitlyStrct = variables.Bitly.shorten( longUrl=, format='json');
if( local.bitlyStrct.status_code eq 200){
local.shortLink =;
// Bitly error handling here
return local.shortLink;

This is a little rough around the edges but it works.

Hey Jon... do you know if this works with 2.2.4? I've gotten as far as:

accessToken = createObject( 'java', 'twitter4j.auth.AccessToken' ).init( 'myConsumerKey', 'myConsumerSecret' );

But get ->
Error: Invalid access token format
Type: java.lang.IllegalArgumentException

Fairly certain I've got twitter4j set up properly, as I can do a writedump( createObject( 'java', 'twitter4j.auth.AccessToken' ) );, which does dump me an object.


Hello Jon, have the same question as Charlie.
I even tried to use javacast to be sure I'm passing java.lang.String keys as parameters, but still have the problem - "Invalid access token format"
Any ideas?


I see, thanks Jon, I will try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment