Search code examples
javatwitter4jtwitter-rest-api

How to get a list of retweeters of a retweet?


The title is confusing, I know, but I do not know how else to phrase this question.

Using twitter4j, I am able to get tweets and the list of users who have retweeted that tweet, like this

However, if the tweet is actually a retweet then I am not able to get the list of retweeters. Example

This is the code I am using to get the list of retweeters:

if(tweet.getId() > 0 && tweet.getRetweetCount() > 0) {
    try {
        List<Status> statuses = twitter.getRetweets(tweet.getId());
        for (Status status : statuses) {
            System.out.println("\n" + "\t" + "Retweeter ID:" + status.getUser().getId() + "\n" +  "\t" + "Retweeter Name:" + status.getUser().getScreenName()); 
        }
    } catch (TwitterException e) {
        //twitter.getRetweeterIds(tweet.getId(), 2, -1);
        e.printStackTrace();
    }
}

How do I get the retweeters of a retweet?


Solution

  • Twitter4J is a Java client library that interfaces with the Twitter REST API. To understand the right call to use it's best to understand the underlying REST API.

    Looking at the Twitter Rest API we can see an API that returns a list of users who have retweeted a particular tweet, GET statuses/retweeters/ids.

    In your code the Twitter4J API you're using, getRetweets(), does not return the IDs of users who retweeted.

    Looking at the Twitter4J Twitter4J API docs we find getRetweeterIds(statusId) that returns the list of user IDs that retweed a particular tweet indicated by statusId.