Created
September 7, 2010 18:16
-
-
Save jonDowdle/568770 to your computer and use it in GitHub Desktop.
Easy way to use twitter4j with ColdFusion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
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 < ***@***.***>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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a little rough around the edges but it works.