如何在LinqToTwitter中获取给定用户关注(而不是关注者)的用户的UserId和ScreenName?
??
答案 0 :(得分:2)
Twitter API使用术语关注者来表示关注用户的人和朋友意味着用户关注的人和LINQ to Twitter继续这种方法。因此,您可以使用Friendship
/ FriendshipType.FriendsList
查询,如下所示:
static async Task FriendsListAsync(TwitterContext twitterCtx)
{
Friendship friendship;
long cursor = -1;
do
{
friendship =
await
(from friend in twitterCtx.Friendship
where friend.Type == FriendshipType.FriendsList &&
friend.ScreenName == "JoeMayo" &&
friend.Cursor == cursor &&
friend.Count == 200
select friend)
.SingleOrDefaultAsync();
if (friendship != null &&
friendship.Users != null &&
friendship.CursorMovement != null)
{
cursor = friendship.CursorMovement.Next;
friendship.Users.ForEach(friend =>
Console.WriteLine(
"ID: {0} Name: {1}",
friend.UserIDResponse, friend.ScreenNameResponse));
}
} while (cursor != 0);
}
此示例以do
/ while
循环遍历结果。请注意,cursor
设置为-1
,它在没有Twitter API游标的情况下从查询开始。每个查询都会分配cursor
,这将获得下一页用户。在if
块中,第一个语句将friendship.CursorMovement.Next
读取到下一页用户的get cursor
。当下一个cursor
为0
时,您已阅读所有关注者。
执行查询后,Users
属性有一个List<User>
,您可以在其中获取用户信息。该演示打印列表中的每个成员。
大型朋友列表可能遇到的一个问题是,Twitter将返回超出速率限制的错误。您可以通过捕获try
并检查超出速率限制的属性,在catch
/ TwitterQueryException
块中捕获此信息。要最小化速率限制问题,请将count
设置为200
,最大值。否则count默认为20.
您可以在LINQ to Twitter网站上下载samples并查看documentation。