{"id":8776,"date":"2012-09-25T23:51:10","date_gmt":"2012-09-25T18:21:10","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=8776"},"modified":"2012-09-26T00:16:34","modified_gmt":"2012-09-25T18:46:34","slug":"get-recent-twitter-followers-using-twitter4j","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/get-recent-twitter-followers-using-twitter4j\/","title":{"rendered":"Get recent twitter followers using Twitter4j"},"content":{"rendered":"<p>Hi,<\/p>\n<p>In the previous <a href=\"http:\/\/www.tothenew.com\/blog\/search-twitter-users-using-twitter4j\/\">blog<\/a> we saw how to fetch twitter users based on some search criteria using the Twitter4j library, a java wrapper available for Twitter API calls.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p>In the same grails project, i had the requirement to display the recent twitter followers of authenticated twitter accounts. Twitter4j library provides a way to get the IDs of twitter followers in the ordered way, which can further be used to get the information of the latest followers. It helped me a lot to make quick list of user&#8217;s recent followers on twitter.<\/p>\n<pre>\r\n\r\n\r\n<\/pre>\n<p>Twitter API needs to have twitter account <strong>access_token<\/strong> and <strong>access_secret<\/strong>, which we obtains after authorizing\/connecting twitter account with the application as mentioned in this <a href=\"http:\/\/www.tothenew.com\/blog\/integrating-java-application-with-twitter\/\">blog<\/a>.<\/p>\n<pre>\r\n\r\n<\/pre>\n<p><strong>Code to fetch recent twitter followers:-<\/strong><\/p>\n<p>[java]<\/p>\n<p>       TwitterFactory factory = new TwitterFactory()<br \/>\n        Twitter twitter = factory.getInstance()<br \/>\n        twitter.setOAuthConsumer(consumerKey, consumerSecret)<br \/>\n        AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)<br \/>\n        twitter.setOAuthAccessToken(accessToken)<br \/>\n        String twitterScreenName = twitter.getScreenName()<br \/>\n        IDs followerIDs = twitter.getFollowersIDs(twitterScreenName, -1)<br \/>\n        List&lt;Integer&gt; followerIdList = followerIDs.IDs<br \/>\n        if (followerIdList?.size() &gt; 0) {<br \/>\n            int numberOfFollowers = Math.min(followerIdList.size(), 20)<br \/>\n            followerIdList = followerIdList.subList(0, numberOfFollowers)<br \/>\n            followerIdList.each {<br \/>\n                twitter4j.User user = twitter.showUser(it)<br \/>\n                  println(&quot;Name: ${user.screenName}&quot;)<br \/>\n                  println(&quot;Location: ${user.location}&quot;)<br \/>\n            }<br \/>\n        }<\/p>\n<p>[\/java]<\/p>\n<p>This code will fetch the recent 20 followers from the user&#8217;s twitter account.<\/p>\n<p>Hope this helps.<\/p>\n<pre>\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hi, In the previous blog we saw how to fetch twitter users based on some search criteria using the Twitter4j library, a java wrapper available for Twitter API calls. In the same grails project, i had the requirement to display the recent twitter followers of authenticated twitter accounts. Twitter4j library provides a way to get [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[700,882,873,1018,1020,1022,1023,1046,1027,1019],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8776"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=8776"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/8776\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=8776"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=8776"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=8776"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}