Twitter api says rate limit is 180 (by user-auth),

2019-07-31 10:50发布

I am using twitter gem to interact with twitter APIs. I am using single user authentication (not application only authentication) as shown here https://github.com/sferik/twitter/blob/master/examples/Configuration.md#single-user-authentication

I have a page, where user clicks to sign in with twitter (I am using http://sign-in-with-twitter.herokuapp.com/)

Once user gets authenticated, I get token and secret and then use my app consumer_key and consumer_secret alongwith signed in user's token and secret to initialize Twitter::REST::Client as shown below

module Ktwitter
  class Manager
    attr_accessor :client
    def initialize token, token_secret
      @client = Twitter::REST::Client.new do |config|
        config.consumer_key        = XXXXXXXXXXXXX
        config.consumer_secret     = ZZZZZZZZZZZZZ
        config.access_token        = token
        config.access_token_secret = token_secret
      end
    end

    def user
      @client.user
    end
  end
end


obj = Ktwitter::Manager.new request.env['omniauth.auth']['credentials']['token'], request.env['omniauth.auth']['credentials']['secret']

I have a (pry) breakpoint and there I run obj.client.user 15 times (in less than 1 minute - just keep hitting) and after that I get rate_limit warning

[1] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[2] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[3] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[4] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[5] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[6] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[7] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[8] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[9] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[10] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[11] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[12] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[13] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[14] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[15] pry(#<T>)> obj.client.user
=> #<Twitter::User id=177777>
[16] pry(#<T>)> obj.client.user
Twitter::Error::TooManyRequests: Rate limit exceeded
from /Users/xyz/.rvm/gems/ruby-2.1.1/gems/twitter-5.8.0/lib/twitter/rest/response/raise_error.rb:17:in `on_complete'

I assume twitter gem client.user makes call to /1.1/users/show.json https://github.com/sferik/twitter/blob/master/lib/twitter/rest/users.rb#L257 ?

This means twitter api gets restriction rate limit on 15 calls in 15 minutes window? But per API documentation https://dev.twitter.com/docs/api/1.1/get/users/show it is 180, not 15. So why am I being restricted after 15 calls itself?

When I uses Twitter API console https://dev.twitter.com/console and there I make call to /1.1/get/users/show and it shows 180 as rate limit. So I am not sure why does twitter gem end up getting limit after 15 calls only.

enter image description here

Also is there anyway, that I can get the whole response header in twitter gem, so that debugging will be easy to read x-rate-limit-limit and x-rate-limit-remaining

Any help will be appreciated.

3条回答
等我变得足够好
2楼-- · 2019-07-31 11:04

I had a similar situation, but with another end point on twitter.

The 15 limit comes from the creation of new client every time you make the call.

Make sure to create a client only once and then make the api calls using that once client.

查看更多
贼婆χ
3楼-- · 2019-07-31 11:05

In case anyone issues the same trouble,

use /1.1/users/show/{user_id}.json request format instead of /1.1/users/show.json

i found the soulution by examinating apigee's requests.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-07-31 11:20

Here it is a more specific way to access the rate limits:

@client = Twitter::REST::Client.new do |config|
  config.consumer_key        = XXXXXXXXXXXXX
  config.consumer_secret     = ZZZZZZZZZZZZZ
  config.access_token        = token
  config.access_token_secret = token_secret
end
response = @client.get('/1.1/application/rate_limit_status.json')

Then you can inspect the response hash.

I hope this helps someone. The current version of the ruby gem is 5.14.0.

查看更多
登录 后发表回答