Ryan Vikander's Development Blog - Twitterhttp://blog.ryanvikander.com/index.cfm
Development blog that may contain but not limited to entries about Coldfusion, Javascript, Flex, and life. Maybe a splash of video games as well.en-usTue, 14 Aug 2018 18:54:14 -0500Thu, 03 Nov 2011 14:13:00 -0500BlogCFChttp://blogs.law.harvard.edu/tech/rssr.vikander@gmail.comr.vikander@gmail.comr.vikander@gmail.comnoUsing Twitter Authentication in an Applicationhttp://blog.ryanvikander.com/index.cfm/2011/11/3/Using-Twitter-Authentication-in-an-Application
I will begin with a little back story. So I manage an internal User Group inside the company I work for (ImageTrend, INC). I proposed a challenge to the attendees to come up with a cool implementation of an app using the Twitter API.
Myself being the proposer I feel I am not eligible for the prize but wanted to take on the challenge just for fun. I found a good blog post by Mr. Ray Camden <a href="http://www.coldfusionjedi.com/index.cfm/2010/9/7/Adding-support-for-automated-tweets-with-OAuth">Adding support for automated tweets with OAuth</a> that was very helpful getting started. I won't go into detail on setting up the Twitter Application on Twitter it's in his blog post. But it's kind of an older post and Twitter4j was released newer versions that do not work with his implementation in the blog. Scanning through the comments on the post, Sean Ryan had posted a blog entry with the updated details using the latest version of Twitter4j. Here is the <a href="http://techblog.troywebconsulting.com/2011/09/authenticating-to-twitter-using-oauth-and-twitter4j-2-2x-api-with-coldfusion/">post</a>
Now to some code examples. Setting up the application cfc I added this code snippet to my onApplicationStart method:
<code>
var paths = [this.siteroot & "twitter4j/twitter4j-core-2.2.6-SNAPSHOT.jar"];
application.javaloader = createObject("component", "javaloader.JavaLoader").init(paths);
application.RequestAccessToken = "Consumer Key From Twitter";
application.RequestAccessSecret = "Consumer Secret From Twitter";
application.Twitter = application.javaloader.create("twitter4j.TwitterFactory").init().getInstance();
application.Twitter.setOAuthConsumer(application.RequestAccessToken,application.RequestAccessSecret);
</code>
My callback URL on Twitter is setup to call http://www.ryanvikander.com/twitter/twitterlogin.cfm?mode=1 I created a CFM page to handle the twitter login. This file looks like this:
<code>
<cfset userInfo = application.userController.getTwitterCredentials(session.userid) />
<cfif userInfo.recordCount EQ 0 AND NOT structKeyExists(url,'mode')>
<cfif NOT structKeyExists(session, "RequestToken")>
<cfset Session.RequestToken = application.Twitter.getOAuthRequestToken(application.twitterlogin)>
</cfif>
<cfset Session.oAuthRequestToken = Session.RequestToken.getToken()>
<cfset Session.oAuthRequestTokenSecret = Session.RequestToken.getTokenSecret()>
<cflocation url="#Session.RequestToken.getAuthorizationURL()#" addtoken="No">
<cfelseif userInfo.recordCount GT 0>
<cfset session.StoredAccessToken = userinfo.token>
<cfset session.StoredAccessSecret = userinfo.secret>
<cflocation url="index.cfm" addtoken="false" />
<cfelse>
<cfset AccessToken = application.Twitter.getOAuthAccessToken(Session.RequestToken,url.oauth_verifier)>
<cfset session.StoredAccessToken = AccessToken.getToken()>
<cfset session.StoredAccessSecret = AccessToken.getTokenSecret()>
<cfset application.userController.saveTwitterCredentials(session.userID,AccessToken.getTokenSecret(), AccessToken.getToken()) />
<cflocation url="index.cfm" addtoken="false"/>
</cfif>
</code>
Very similar to Ray's code for the one time authentication but I wanted to database the tokens that Twitter returned so in the else if I check to see if the user exists and I have stored their token if they do not exist I database the token and secret so they don't have to authorize the app every time.
Once this is complete you can do pretty much whatever is available from the <a href="http://twitter4j.org/en/index.html">Twitter4J</a> class library.
I apoligize if this is a scattered post wanted to do a quick write up. If you have any questions/concerns I am more than willing to try and answer.
TwitterColdfusionThu, 03 Nov 2011 14:13:00 -0500http://blog.ryanvikander.com/index.cfm/2011/11/3/Using-Twitter-Authentication-in-an-Application