---
title: "Using twitter4j with Play! Framework and Secure Social is this easy"
date: 2013-02-01T14:05:13+00:00
author: "poornerd"
canonical: https://www.poornerd.com/2013/02/01/using-twitter4j-w-play-framework-and-secure-social-is-this-easy/
source: Raw Markdown twin of the HTML article; content is the original source.
---
[<img class="alignleft size-medium wp-image-185" alt="Screen Shot 2013-02-01 at 14.54.45" src="https://www.poornerd.com/wp-content/uploads/2013/02/Screen-Shot-2013-02-01-at-14.54.45-269x300.png" width="269" height="300" srcset="https://www.poornerd.com/wp-content/uploads/2013/02/Screen-Shot-2013-02-01-at-14.54.45-269x300.png 269w, https://www.poornerd.com/wp-content/uploads/2013/02/Screen-Shot-2013-02-01-at-14.54.45.png 431w" sizes="(max-width: 269px) 100vw, 269px" />](https://www.poornerd.com/wp-content/uploads/2013/02/Screen-Shot-2013-02-01-at-14.54.45.png)During yesterday&#8217;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 <a href="http://securesocial.ws/guide/getting-started.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/securesocial.ws');" target="_blank">http://securesocial.ws/guide/getting-started.html</a> and only takes a minute)
  3. Add the dependecy to <a href="http://twitter4j.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/twitter4j.org');" target="_blank">twitter4j</a> 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 &#8211; because you are using the InMemoryUserService none of the Authentication data is stored &#8211; 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&#8230;
