Fetching User’s Home Timeline using Twitter4j

25 / Sep / 2012 by Vishal Sahu 1 comments

Hi,

In the previous blog we saw how to post status update on Twitter using Twitter4j library.


In the same project i had the requirement to show Twitter home timeline inside our application. I used the same java wrapper library to retrieve tweets from the user twitter account. In this blog we will see how to retrieve the Users Timeline using API calls.


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 Twitter User Home Timeline :-

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

           int totalTweets = 20 // no of tweets to be fetched
           Paging paging = new Paging(1, totalTweets)
           List tweets = twitter.getHomeTimeline(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 from the User Timeline using API calls.

Hope this helps.


Tag -

FOUND THIS USEFUL? SHARE IT

comments (1 “Fetching User’s Home Timeline using Twitter4j”)

  1. Mahen Rathod

    Hi Vishal,

    nice post indeed. I have the requirement to fetch all the tweet of another user (not me) including his/her friends tweets.

    Ex, the home timeline that we see on our homepage. I want the same list of tweets of another user.

    Thanks.

    Reply

Leave a comment -