Search code examples
ruby-on-railstwitter

Twitter::Error::TooManyRequests in GetFollowersController#index


How to solve this problem? I am using 'gem twitter'. But I can not get a list of followers:

@client.followers.collect {|f| @client.user(f).screen_name }  #=> Twitter::Error::TooManyRequests (Rate limit exceeded):

Solution

  • It looks like you're trying to collect the screen_names of your followers. However, in your collect block, you're calling the Twitter API again. This results in many API calls to Twitter; and hits your rate limit.

    You don't need to do it that way. When you make the @client.followers call, you already have the screen_names of your followers. Try this:

    @client.followers.map { |follower| follower.screen_name }
    

    You can look at the API documentation for GET followers/list to see what else is in there. It also tells you that it's rate limited to 15 requests in a 15 minute window. If you've been testing your code a few times, it can be pretty easy to hit that limit.

    Another concern is Twitter only returns a maximum of 200 followers per call. If you have more than 200 followers, you'll need to make multiple API calls to retrieve all followers. If you have more than 3,000 followers, it may not be possible to retrieve all followers within the 15 minute rate-limit window.

    The twitter gem handles the multiple API calls for you. For example, if you have 1,000 followers, the gem will make multiple API calls behind the scenes. The gem has a recommended way to handle rate limits. Here's what they recommend:

    follower_ids = client.follower_ids('justinbieber') begin  
      follower_ids.to_a
    rescue Twitter::Error::TooManyRequests => error
      # NOTE: Your process could go to sleep for up to 15 minutes but if you  
      # retry any sooner, it will almost certainly fail with the same exception.
      sleep error.rate_limit.reset_in + 1
      retry
    end
    

    That error is saying you've made too many requests within a given time frame. You'll have to wait until your rate limit has been cleared.

    This is what Twitter says:

    Rate limiting of the API is primarily on a per-user basis — or more accurately described, per user access token. If a method allows for 15 requests per rate limit window, then it allows 15 requests per window per access token.

    See: https://developer.twitter.com/en/docs/basics/rate-limiting