poornerd

my thoughts on programming and other nerdy stuff

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

| 2 Comments

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() {
    // The factory instance is re-useable and thread safe.
    TwitterFactory factory = new TwitterFactory();
    Twitter twitter = new TwitterFactory().getInstance();twitter.setOAuthConsumer(Play.application().configuration().getString("securesocial.twitter.consumerKey"), Play.application().configuration().getString("securesocial.twitter.consumerSecret"));
    twitter4j.auth.AccessToken accessToken = new twitter4j.auth.AccessToken(token(), tokenSecret());
    twitter.setOAuthAccessToken(accessToken);
    return twitter;
    }
    public static String tokenSecret() {
    String retval = "";
    scala.collection.Iterator iterator = Application.getCurrentUser().oAuth1Info().iterator();
    while (iterator.hasNext()) {
    OAuth1Info oAuth1Info = iterator.next();
    retval = oAuth1Info.secret();
    }
    return retval;
    }
    public static String token() {
    String retval = "";
    scala.collection.Iterator iterator = Application.getCurrentUser().oAuth1Info().iterator();
    while (iterator.hasNext()) {
    OAuth1Info oAuth1Info = iterator.next();
    retval = oAuth1Info.token();
    }
    return retval;
    }
    public static Identity getCurrentUser() {
    return (Identity) ctx().args.get(SecureSocial.USER_KEY);
    }
  6. Then I added some code in my Controller to list (for example) my Followers

    long cursor = -1;
    IDs ids;
    System.out.println("Listing following ids.");
    do {
    ids = twitter.getFriendsIDs(cursor);
    for (long id : ids.getIDs()) {
    twitter4j.User twitterUser = twitter.showUser(id);
    twitterUsers.put(twitterUser.getScreenName(), new TwitterUser(id,twitterUser));
    System.out.println(id);
    }
    } while ((cursor = ids.getNextCursor()) != 0);

Yes, that is it…

If you have read this far, you may as well follow me on Twitter:

Author: poornerd

Tech­nol­o­gist, Entre­pre­neur, Vision­ary, Pro­gram­mer :: Grad­u­ated from USC (Uni­ver­sity of South­ern Cal­i­for­nia) with a degree in Com­puter Sci­ence. After 10+ years of free­lance con­sult­ing and pro­gram­ming, he co-founded Site­Force AG eBusi­ness Solu­tions in 1999 in Munich (München), Ger­many.

2 Comments

  1. Thank you, this was really helpful

  2. Ok now I want to see you test your methods that are secured by SecureSocial..
    That is the only downfall of SecureSocial, they have not provided a way to test the secured methods you have to find your own way to fake a login during testing.

Leave a Reply

Required fields are marked *.