Fetching twitter user ‘Favorite’ tweets using Twitter4j
Hi,
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 :-
[java]
TwitterFactory factory = new TwitterFactory()
Twitter twitter = factory.getInstance()
twitter.setOAuthConsumer(consumerKey, consumerSecret)
AccessToken accessToken = new AccessToken(twitterToken, twitterSecret)
twitter.setOAuthAccessToken(accessToken)
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}"
}
[/java]
So, this code will fetch the latest 20 tweets which user has marked as favorite using API calls.
Hope this helps.