如何获取给定用户在 LinqToTwitter 中关注(不是关注者)的用户的 UserId 和 ScreenName?
??
最佳答案
Twitter API 使用术语关注者来表示关注用户的人,使用 friend 来表示用户关注的人,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
block 中,第一条语句读取 friendship.CursorMovement.Next
获取cursor
供下一页用户使用。当下一个cursor
是 0
,您已阅读所有关注者。
查询执行后,Users
属性有 List<User>
您可以在其中获取用户信息。该演示打印列表中的每个成员。
对于大型好友列表,您可能会遇到的问题之一是 Twitter 将因超出速率限制而返回错误。您将能够在 try
中捕捉到这一点。/catch
阻止,通过捕获 TwitterQueryException
并检查属性是否超出速率限制。要最大限度地减少速率限制问题,请设置 count
至200
,最大。否则计数默认为 20。
您可以下载samples并查看documentation有关此信息,请访问 LINQ to Twitter 网站。
关于twitter - 获取给定用户在 LinqToTwitter 中关注的用户(不是关注者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46284041/