Retrieving retweets by twitter user using Twitter4j

25 / Sep / 2012 by Vishal Sahu 0 comments

Hi,

In the previous blog we saw how to fetch Twitter user’s Timeline using API calls with the help of Twitter4j library.



I had a requirement to display all the retweets by the authenticating user and display them in our application. In this blog we will see how to retrieve the retweets by any twitter user and use them in our application.


For making any twitter API calls we need to have twitter account access_token and access_secret, which we obtains after authorizing/connecting twitter account with the application as mentioned in this blog.


Code to fetch retweets of twitter user :-

[java]
TwitterFactory factory = new TwitterFactory()
Twitter twitter = factory.getInstance()
twitter.setOAuthConsumer(consumerKey, consumerSecret)
AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)
twitter.setOAuthAccessToken(accessToken)

List tweets = twitter.getRetweetsOfMe(paging)
tweets.each {tweet ->
println "ID : ${tweet.id}"
if (tweet?.isRetweet()) {
println "Original Source : ${tweet.retweetedStatus.user.screenName}"
}
println "Tweet : ${tweet.text}"
}
[/java]

So, this code will fetch the latest 20 retweets from the User Timeline using API calls.

Hope this helps.


FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *