Search code examples
javatwittertwitter4j

How to get followers and following list screen names (Twitter4J)?


I am trying to get my followers and following list via Twitter 4J. I pass the configuration builder from the main method as a parameter to the method.

Here is the code:

try {
            Twitter twitter = new TwitterFactory().getInstance();
            long cursor = -1;
            IDs ids;
            System.out.println("Listing following ids.");
            do {
                if (0 < args.length) {
                    ids = twitter.getFriendsIDs(args[0], cursor);
                } else {
                    ids = twitter.getFriendsIDs(cursor);
                }
                for (long id : ids.getIDs()) {
                    System.out.println(id);
                }
            } while ((cursor = ids.getNextCursor()) != 0);
            System.exit(0);
        } catch (TwitterException te) {
            te.printStackTrace();
            System.out.println("Failed to get friends' ids: " + te.getMessage());
            System.exit(-1);
        }

So far this only returns IDS but it does not return the screen name of the followers. Does anyone have any idea? I am working in Java and printing to the terminal.

Thanks.


Solution

  • String twitterScreenName = twitter.getScreenName();
    PagableResponseList<User> statuse = twitter.getFollowersList(twitterScreenName, -1);
                for (User follower : statuse) {
    
                    System.out.println(follower.getName()); 
                }