Search code examples
twittertwitter4j

get tweets by particular user using twitter4j java api


I want to get tweets by particular user by entering its userId.

I can search for a text by:

     Query query = new Query("Hi");
        QueryResult result;
        do {
            result = twitter.search(query);
            List<Status> tweets = result.getTweets();
            for (Status tweet : tweets) {
                System.out.println("@" + tweet.getUser().getScreenName() + 
                     " - " + tweet.getText());
                }
            } while ((query = result.nextQuery()) != null); 

but how can I search for the tweets by entering particular userId, is there any direct method or I have to apply logic ?

I am trying:

            Status status = twitter.showStatus(id);
            if (status != null){
                System.out.println("@" + status.getUser().getScreenName()
                + " - " + status.getText());}

where id is userId, but by doing this, I am getting the error:

Failed to search tweets: 404:The URI requested is invalid or the resource requested, such as a user, does not exists. Also returned when the requested format is not supported by the requested method. message - No status found with that ID. code - 144

Can anyone please help me with this?


Solution

  • With the Twitter API you can get up to ~3200 tweets from an user, to do this you can get the time line from an specific user, see those questions

    By the way, you are getting that error because you are using twitter.showStatus(id); with an userid, you need to call twitter.showUser(id) and you won't get that error