Fetching twitter user ‘Favorite’ tweets using Twitter4j

25 / Sep / 2012 by Vishal Sahu 0 comments


In the previous bog, we saw how to fetch the Retweets by any twitter using using Twitter4j Java library for making twitter API calls.

I had the requirement to fetch latest tweets from user timeline which user has marked as ‘Favorite’ and show them in our application. So, in this blog we will see how to retrieve latest tweets which user has marked as Favorite. The user needs to be authenticated to our application before making API calls to retrieve such data.

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 Favorite tweets of twitter user :-

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

int totalTweets = page * 20
Paging paging = new Paging(1, totalTweets)
List tweets = twitter.getFavorites(paging)
tweets.each {tweet ->
println "ID : ${tweet.id}"
println "User : ${tweet.user.screenName}"
println "Tweet : ${tweet.text}"

So, this code will fetch the latest 20 tweets which user has marked as favorite using API calls.

Hope this helps.


Leave a Reply

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