Skip to content

Instantly share code, notes, and snippets.

@jonDowdle
Created September 7, 2010 18:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jonDowdle/568770 to your computer and use it in GitHub Desktop.
Save jonDowdle/568770 to your computer and use it in GitHub Desktop.
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="">
<cfscript>
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.TwitterConsumerKey,
variables.TwitterConsumerSecret
);
variables.Twitter = createObject("java", "twitter4j.TwitterFactory").getInstance(
local.accessToken );
if( len(variables.authKey) gt 0 && len(variables.authSecret) gt 0 ){
variables.Twitter.setOAuthAccessToken(
variables.authKey,
variables.authSecret);
}
return this;
</cfscript>
</cffunction>
<cffunction name="getAuthorizationURL">
<cfargument name="callbackUrl" required="false">
<cfset var localCallbackUrl = 'yourCallbackUrl'>
<cfif isDefined("arguments.callbackUrl")>
<cfset localCallbackUrl = arguments.callbackUrl>
</cfif>
<cfset variables.RequestToken = variables.Twitter.getOAuthRequestToken(localCallbackUrl)>
<cfset saveRequestToken(variables.RequestToken )>
<cfreturn variables.RequestToken.getAuthorizationURL()>
</cffunction>
<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>
<cffunction name="saveAuthAccessToken">
<cfargument name="oAuthVerifier" required="yes">
<cfscript>
var accessToken = variables.Twitter.getOAuthAccessToken(
client.oAuthRequestToken,
client.oAuthRequestTokenSecret,
arguments.oAuthVerifier);
variables.Twitter.setOAuthAccessToken(accessToken);
<!---
You'll want to save these 2 values somewhere persistent
variables.Twitter.getOAuthAccessToken().getToken()
variables.Twitter.getOAuthAccessToken().getTokenSecret()
--->
</cfscript>
<cfreturn accessToken>
</cffunction>
<cffunction name="getAuthAccessToken">
<cfreturn variables.Twitter.getOAuthAccessToken()>
</cffunction>
<cffunction name="updateStatus">
<cfargument name="newStatus" required="true">
<cfset variables.Twitter.updateStatus("#arguments.newStatus#")>
</cffunction>
<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#()")>
</cffunction>
</cfcomponent>
<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>
<cffunction name="sendTweet">
<cfargument name="message">
<cfargument name="link">
<cfargument name="hashtags" type="array">
<cfscript>
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] & " ";
}else{
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#");
</cfscript>
</cffunction>
<cffunction name="shortenLink">
<cfargument name="link">
<cfscript>
var local = {};
local.shortLink = '';
local.bitlyStrct = variables.Bitly.shorten( longUrl= arguments.link, format='json');
if( local.bitlyStrct.status_code eq 200){
local.shortLink = local.bitlyStrct.data.url;
}else{
// Bitly error handling here
}
return local.shortLink;
</cfscript>
</cffunction>
</cfcomponent>
@jonDowdle
Copy link
Author

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

@charliegriefer
Copy link

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.

Thx!

@SkyStalker
Copy link

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?
Thanks

@jonDowdle
Copy link
Author

jonDowdle commented Oct 12, 2011 via email

@SkyStalker
Copy link

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