Using twitter4j with Play! Framework and Secure Social is this easy

Posted by Brian Porter on February 01, 2013

Screen Shot 2013-02-01 at 14.54.45During yesterday’s personal Hackathon, I started a project which I might introduce here sometime.  But the coolest revelation was (again) how easy it was to get up and running.

  1. Create a new Play Project
  2. Add Secure Social and configure it for Twitter, and use the InMemoryUserService from the examples.  (all this is described here http://securesocial.ws/guide/getting-started.html and only takes a minute)
  3. Add the dependecy to twitter4j to your Build.scala like this:
`"org.twitter4j"% "twitter4j-core"% "3.0.3"`   4. Secure your controller action method to force the (Login) Authentication with Twitter.  Remember – because you are using the InMemoryUserService none of the Authentication data is stored – you will have to reconnect each time.
  
`@SecureSocial.SecuredAction`   5. I then added these standard methods to get the Authenticated Twitter User, Token, Secret and the twitter4J Connection: (The tokenSecret, token and current User are coming from the Secure Social Oauth1 Connection, and are used to authenticate the Twitter Connection.
  
`public static Twitter getTwitterInstance() {<br /> // The factory instance is re-useable and thread safe.<br /> TwitterFactory factory = new TwitterFactory();<br /> Twitter twitter = new TwitterFactory().getInstance();twitter.setOAuthConsumer(Play.application().configuration().getString("securesocial.twitter.consumerKey"), Play.application().configuration().getString("securesocial.twitter.consumerSecret"));<br /> twitter4j.auth.AccessToken accessToken = new twitter4j.auth.AccessToken(token(), tokenSecret());<br /> twitter.setOAuthAccessToken(accessToken);<br /> return twitter;<br /> }<br /> public static String tokenSecret() {<br /> String retval = "";<br /> scala.collection.Iterator iterator = Application.getCurrentUser().oAuth1Info().iterator();<br /> while (iterator.hasNext()) {<br /> OAuth1Info oAuth1Info = iterator.next();<br /> retval = oAuth1Info.secret();<br /> }<br /> <span style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif;">return retval;</span><br /> }<br /> public static String token() {<br /> String retval = "";<br /> scala.collection.Iterator iterator = Application.getCurrentUser().oAuth1Info().iterator();<br /> while (iterator.hasNext()) {<br /> OAuth1Info oAuth1Info = iterator.next();<br /> retval = oAuth1Info.token();<br /> }<br /> return retval;<br /> }<br /> public static Identity getCurrentUser() {<br /> return (Identity) ctx().args.get(SecureSocial.USER_KEY);<br /> }<br /> `    6. Then I added some code in my Controller to list (for example) my Followers
  
`<br /> long cursor = -1;<br /> IDs ids;<br /> System.out.println("Listing following ids.");<br /> do {<br /> ids = twitter.getFriendsIDs(cursor);<br /> for (long id : ids.getIDs()) {<br /> twitter4j.User twitterUser = twitter.showUser(id);<br /> twitterUsers.put(twitterUser.getScreenName(), new TwitterUser(id,twitterUser));<br /> System.out.println(id);<br /> }<br /> } while ((cursor = ids.getNextCursor()) != 0);<br /> ` 

Yes, that is it…

If you made it this far, you may as well follow me on LinkedIn: Follow Brian Porter