Archive for October, 2011

ColdFusion + twitter4j 2.2.4 Authentication

October 15, 2011 Leave a comment

After seeing an interesting question about twitter authentication on, I decided to take my first look at the Twitter4j library.  The Twitter4j Code Examples and Adding support for automated tweets with OAuth  were particularly helpful.  But apparently version 2.2.4 changed a few things.  So I thought I would post the small tweaks to the example that worked for me:


<!--- the factory and consumer keys could be stored in the application scope --->
<cfset Twitter = createObject("java", "twitter4j.TwitterFactory").getInstance()>
<cfset Twitter.setOAuthConsumer( "MyConsumerKey", "MyConsumerSecret")>

<cfif NOT structKeyExists(url, "oauth_verifier")>
<!--- // 2. Authorize --->
<cfset Session.RequestToken = Twitter.getOAuthRequestToken( "MyCallbackURL")>
<cflocation url="#Session.RequestToken.getAuthorizationURL()#" addtoken="No">

<!--- // 3. Authenticate // --->
<!--- Twitter returns a "verifier" key in the callback URL --->
<!--- Use it along with the <span pre="the ">requestToken</span> to extract the accessToken --->
<cfset AccessToken = Twitter.getOAuthAccessToken(Session.RequestToken, url.oauth_verifier)>
<cfset session.StoredAccessToken = AccessToken.getToken()>
<cfset session.StoredAccessSecret = AccessToken.getTokenSecret()>

<!--- Delete the one time <span pre="time ">RequestToken</span> as it is no longer needed --->
<cfset structDelete(session, "RequestToken")>
<cfdump var="#session#" label="Stored AccessToken Values">
Categories: ColdFusion, Java Tags: ,