Get tweets posted by user using Twitter4j

25 / Sep / 2012 by Vishal Sahu 0 comments

Hi,

In the previous blog we saw how to fetch tweets in which a particular user is mentioned, using the Twitter4j java wrapper library available for Twitter API calls.


In the same project, i had the requirement to display the tweets posted by particular user including retweets. I searched for it and found a cool way to achieve this using Twitter4j library.



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 Tweets by 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.getUserTimeline(paging)
tweets.each {tweet ->
println "ID : ${tweet.id}"
println "User : ${tweet.user.screenName}"
println "Tweet : ${tweet.text}"
}
[/java]

This code will fetch the latest 20 tweets which user has posted using API calls.

Hope this helps.


Tag -

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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