public
Last active

Easy way to use twitter4j with ColdFusion

  • Download Gist
Twitter.cfc
ColdFusion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
<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>
TwitterHelper.cfc
ColdFusion
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
<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>

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.

Thx!

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

No, I'm pretty sure it doesn't work with 2.4 and that's why you're getting
that error. Try 2.1 and see how that works. I'd love to get around to fixing
this but I haven't had the time.

On Wed, Oct 12, 2011 at 4:18 PM, SkyStalker <
reply@reply.github.com>wrote:

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

Reply to this email directly or view it on GitHub:
https://gist.github.com/568770

I see, thanks Jon, I will try

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.