Search code examples
twitter4j

how can I get followers for a particular userID using Twitte


I want to get followers Id's for a particular userId using java program. where I want to implement the cursor concept with rate limit set ... Can any one post me the code.


Solution

  • Use the following code snippet to get follower id. After getting the ids you use show user to get other details. Remember to use this code in background thread like in asynctask.

          long[] tempids = null;
          ConfigurationBuilder config =
                new ConfigurationBuilder()
                        .setOAuthConsumerKey(custkey)
                        .setOAuthConsumerSecret(custsecret)
                        .setOAuthAccessToken(accesstoken)
                        .setOAuthAccessTokenSecret(accesssecret);
    
        twitter1 = new TwitterFactory(config.build()).getInstance();
        while(cursor != 0) {
    
                try {
                    IDs temp    = twitter1.friendsFollowers().getFollowersIDs("username", cursor);
                    cursor      = temp.getNextCursor();
                    tempids = temp.getIDs();
                } catch (twitter4j.TwitterException e) {
                    System.out.println("twitter: failed");
                    e.printStackTrace();
                    return null;
                }
    
                if(tempids != null) {
                    for (long id : tempids) {
                        ids.add(id);
                        System.out.println("followerID: " + id);
                    }
                }
            }