2013-03-22 26 views
1

我試圖通過使用TweetSharp庫獲得所有粉絲。在twitter的api中,它表示我們有機會使用遊標變量來做分頁,以便我們能夠獲得所有的關注者。在Twitter上與TweetSharp獲取所有粉絲

https://dev.twitter.com/docs/api/1.1/get/followers/list

https://dev.twitter.com/docs/misc/cursoring

但是,有沒有機會做這個操作與TweetSharp?我正在寫下面的代碼:

var options = new ListFollowersOptions { ScreenName = input }; 
IEnumerable<TwitterUser> friends = service.ListFollowers(options); 

但是,這隻返回前20,那麼我將無法跟蹤任何朋友。

如果你能幫助我,那會很棒。

謝謝。

回答

1

我在.NET 4.0 WinForms應用程序中使用TweetSharp v2.3.0。這裏是適合我的:

// this code assumes that your TwitterService is already properly authenticated 

TwitterUser tuSelf = service.GetUserProfile(
    new GetUserProfileOptions() { IncludeEntities = false, SkipStatus = false }); 

ListFollowersOptions options = new ListFollowersOptions(); 
options.UserId = tuSelf.Id; 
options.ScreenName = tuSelf.ScreenName; 
options.IncludeUserEntities = true; 
options.SkipStatus = false; 
options.Cursor = -1; 
List<TwitterUser> lstFollowers = new List<TwitterUser>(); 

TwitterCursorList<TwitterUser> followers = service.ListFollowers(options); 

// if the API call did not succeed 
if (followers == null) 
{ 
    // handle the error 
    // see service.Response and/or service.Response.Error for details 
} 
else 
{ 
    while (followers.NextCursor != null) 
    { 
     //options.Cursor = followers.NextCursor; 
     //followers = m_twService.ListFollowers(options); 

     // if the API call did not succeed 
     if (followers == null) 
     { 
      // handle the error 
      // see service.Response and/or service.Response.Error for details 
     } 
     else 
     { 
      foreach (TwitterUser user in followers) 
      { 
       // do something with the user (I'm adding them to a List) 
       lstFollowers.Add(user); 
      } 
     } 

     // if there are more followers 
     if (followers.NextCursor != null && 
      followers.NextCursor != 0) 
     { 
      // then advance the cursor and load the next page of results 
      options.Cursor = followers.NextCursor; 
      followers = service.ListFollowers(options); 
     } 
     // otherwise, we're done! 
     else 
      break; 
    } 
} 

注意:這可能被認爲是重複的問題。 (請參閱herehere。)但是,這些現有問題似乎與TweetSharp的不同(較舊版本)版本有關,因爲版本2.3.0中沒有TwitterService.ListFollowersOf()方法。

+1

謝謝..按預期工作..我注意到,使用v1.1 Twitter只允許你15請求/ 15分鐘..見這裏https://dev.twitter.com/docs/rate-limiting/1.1/限制,所以你可以得到的最大f鳥數量是15分鐘內的300): – hriziya 2013-06-29 09:55:20

+0

@HiteshRiziya你可以使用[GET追隨者/ ids](https://dev.twitter.com/docs/api/1.1/get/followers/ids )與[GET用戶/查找](https://dev.twitter.com/docs/api/1.1/get/users/lookup)在15分鐘內獲得18000個關注者。 – 2013-07-10 19:19:25