2015-06-18 81 views
2

我試圖使用屏幕名稱獲取Twitter追隨者。但是,我只能得到我的追隨者屏幕名稱,因爲我期待着我的追隨者的追隨者。但是我沒有找到任何支持的方法。Java代碼使用Twitter的屏幕名稱獲取任何用戶的Twitter追隨者

我的代碼如下。

TwitterFactory factory = new TwitterFactory(); 
Twitter twitter = factory.getInstance(); 
twitter.setOAuthConsumer(consumerKey, consumerSecret); 
AccessToken accessToken = new AccessToken(twitterToken, twitterSecret); 
twitter.setOAuthAccessToken(accessToken); 
String twitterScreenName = twitter.getScreenName(); 
IDs followerIDs = twitter.getFollowersIDs(twitterScreenName, -1); 
long[] ids = followerIDs.getIDs(); 
for (long id : ids) { 
    twitter4j.User user = twitter.showUser(id); 
    //here i am trying to fetch the followers of each id 
    System.out.println("Name: " + user.getScreenName()); 
    System.out.println("Location:" + user.getLocation()); 
} 

任何人都可以請幫助我。

+0

[getFollowersIds](http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersIDs(長,%20long))或[getFollowersList] (http://twitter4j.org/javadoc/twitter4j/api/FriendsFollowersResources.html#getFollowersList(long,%20long))通過userId? – Rup

回答

1

您需要在這裏進行嵌套。您剛剛獲得當前用戶關注者的列表。但是你需要得到你追隨者的追隨者名單。

示例代碼如下:

TwitterFactory factory = new TwitterFactory(); 
    Twitter twitter = factory.getInstance(); 
    String twitterScreenName; 
    try { 
     twitterScreenName = twitter.getScreenName(); 

    IDs followerIDs = twitter.getFollowersIDs(twitterScreenName, -1); 
    long[] ids = followerIDs.getIDs(); 
    for (long id : ids) { 
     twitter4j.User user = twitter.showUser(id); 
     //here i am trying to fetch the followers of each id 
     String userScreenName = user.getScreenName(); 
     System.out.println("Name: " + user.getScreenName()); 
     System.out.println("Location:" + user.getLocation()); 

     IDs followerIDsOfFollowers = twitter.getFollowersIDs(user.getScreenName(), -1); 
     long[]fofIDs = followerIDsOfFollowers.getIDs(); 
     for(long subId : fofIDs) { 
      twitter4j.User user1 = twitter.showUser(subId); 
      System.out.println("Follower Master:" + userScreenName +" Follower of Follower Name: " + user1.getScreenName()); 
      System.out.println("Location:" + user1.getLocation()); 

     } 
+0

謝謝abhijeet dhumal。它的工作。 – Manindar

+0

費率限制錯誤 –

相關問題