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 Adding support for automated tweets with OAuth 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 post
Now to some code examples. Setting up the application cfc I added this code snippet to my onApplicationStart method:
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();
var paths = [this.siteroot & "twitter4j/twitter4j-core-2.2.6-SNAPSHOT.jar"];
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:
<cfif userInfo.recordCount EQ 0 AND NOT structKeyExists(url,'mode')>
<cfif NOT structKeyExists(session, "RequestToken")>
<cfset Session.RequestToken = application.Twitter.getOAuthRequestToken(application.twitterlogin)>
<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" />
<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"/>
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 Twitter4J 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.