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

Dur­ing yesterday’s per­sonal Hackathon, I started a project which I might intro­duce here some­time. But the coolest rev­e­la­tion was (again) how easy it was to get up and running.

  1. Cre­ate a new Play Project
  2. Add Secure Social and con­fig­ure it for Twit­ter, and use the InMem­o­ryUserSer­vice from the exam­ples. (all this is described here http://securesocial.ws/guide/getting-started.html and only takes a minute)
  3. Add the depen­decy to twitter4j to your Build.scala like this:
     
     

    'org.twitter4j'% 'twitter4j-core'% '3.0.3'
  4. Screen Shot 2013-02-01 at 14.54.45

  5. Secure your con­troller action method to force the (Login) Authen­ti­ca­tion with Twit­ter. Remem­ber — because you are using the InMem­o­ryUserSer­vice none of the Authen­ti­ca­tion data is stored — you will have to recon­nect each time.
    @SecureSocial.SecuredAction
  6. I then added these stan­dard meth­ods to get the Authen­ti­cated Twit­ter User, Token, Secret and the twitter4J Con­nec­tion: (The tokenSe­cret, token and cur­rent User are com­ing from the Secure Social Oauth1 Con­nec­tion, and are used to authen­ti­cate the Twit­ter Con­nec­tion.
     
     
     
     
     
     

    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);
    }
    
  7. Then I added some code in my Con­troller to list (for exam­ple) my Fol­low­ers
    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…
 

Reference: Using twitter4j with Play! Framework and Secure Social is this easy from our JCG partner Brian Porter at the Poornerd blog.

Related Whitepaper:

Functional Programming in Java: Harnessing the Power of Java 8 Lambda Expressions

Get ready to program in a whole new way!

Functional Programming in Java will help you quickly get on top of the new, essential Java 8 language features and the functional style that will change and improve your code. This short, targeted book will help you make the paradigm shift from the old imperative way to a less error-prone, more elegant, and concise coding style that’s also a breeze to parallelize. You’ll explore the syntax and semantics of lambda expressions, method and constructor references, and functional interfaces. You’ll design and write applications better using the new standards in Java 8 and the JDK.

Get it Now!  

Leave a Reply


5 + = six



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books