Home > ColdFusion, Java > ColdFusion + twitter4j 2.2.4 Authentication

ColdFusion + twitter4j 2.2.4 Authentication

After seeing an interesting question about twitter authentication on stackoverflow.com, 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: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: