使用TweetSharp在Twitter上获取所有关注者

时间:2013-03-22 12:49:24

标签: twitter tweetsharp

我正试图通过TweetSharp库获取所有关注者。在twitter的api中,它表示我们有机会使用游标变量来进行分页,这样我们就可以获得所有关注者。

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 个答案:

答案 0 :(得分: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()方法。