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:
1var paths = [this.siteroot & "twitter4j/twitter4j-core-2.2.6-SNAPSHOT.jar"];
2 application.javaloader = createObject("component", "javaloader.JavaLoader").init(paths);
4 application.RequestAccessToken = "Consumer Key From Twitter";
5 application.RequestAccessSecret = "Consumer Secret From Twitter";
7 application.Twitter = application.javaloader.create("twitter4j.TwitterFactory").init().getInstance();
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:
1<cfset userInfo = application.userController.getTwitterCredentials(session.userid) />
3<cfif userInfo.recordCount EQ 0 AND NOT structKeyExists(url,'mode')>
4 <cfif NOT structKeyExists(session, "RequestToken")>
5 <cfset Session.RequestToken = application.Twitter.getOAuthRequestToken(application.twitterlogin)>
8 <cfset Session.oAuthRequestToken = Session.RequestToken.getToken()>
9 <cfset Session.oAuthRequestTokenSecret = Session.RequestToken.getTokenSecret()>
11 <cflocation url="#Session.RequestToken.getAuthorizationURL()#" addtoken="No">
12<cfelseif userInfo.recordCount GT 0>
13 <cfset session.StoredAccessToken = userinfo.token>
14 <cfset session.StoredAccessSecret = userinfo.secret>
16 <cflocation url="index.cfm" addtoken="false" />
18 <cfset AccessToken = application.Twitter.getOAuthAccessToken(Session.RequestToken,url.oauth_verifier)>
19 <cfset session.StoredAccessToken = AccessToken.getToken()>
20 <cfset session.StoredAccessSecret = AccessToken.getTokenSecret()>
22 <cfset application.userController.saveTwitterCredentials(session.userID,AccessToken.getTokenSecret(), AccessToken.getToken()) />
24 <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.