我正在尝试使用以下代码片段获取所有关注者列表。每个电话都会获得 200 个关注者,所以我在循环中结束以获得所有关注者。用户有 23.1K 关注者,但当关注者达到 2800 时,我收到“速率限制”超出错误。我发现 twitter 允许每个用户 15 个请求,有什么方法可以修复以下代码以获取所有关注者?
private static async Task<List<User>> GetTwitterFollowersAsync(
ulong twitterUserId, SingleUserAuthorizer auth, int? maxFollowers)
{
var followerss = maxFollowers ?? 15000;
long nextCursor = -1;
var users = new List<User>();
try
{
while (nextCursor != 0)
{
var twitterCtx = new TwitterContext(auth);
var friends = await twitterCtx.Friendship
.Where(f => f.Type == FriendshipType.Show
&& f.SourceScreenName == "John_Papa"
&& f.Count == followerss && f.Cursor == nextCursor)
.Select(f => new TwitterData()
{
NewCursor = f.CursorMovement.Next,
Followers = f.Users.Where(t => !t.Protected)
.Take(followerss).Select(s => s).ToList()
})
.SingleOrDefaultAsync();
nextCursor = friends.NewCursor;
users.AddRange(friends.Followers);
}
return users;
}
catch (Exception ex)
{
return null;
}
}