Twitter api表示速率限制为180(通过用户身份验证),但使用twitter gem限制为仅15个调用 [英] Twitter api says rate limit is 180 (by user-auth), but using twitter gem restrict to 15 calls only

查看:259
本文介绍了Twitter api表示速率限制为180(通过用户身份验证),但使用twitter gem限制为仅15个调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用twitter gem与twitter API进行交互.我正在使用single user authentication(不是仅限应用程序身份验证),如此处所示 https://github.com/sferik/twitter/blob/master/examples/Configuration.md#single-user-authentication

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

我有一个页面,用户单击该页面即可使用Twitter登录(我正在使用 http ://sign-in-with-twitter.herokuapp.com/)

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

用户通过身份验证后,我得到tokensecret,然后使用我的应用程序consumer_keyconsumer_secret以及登录用户的tokensecret来初始化Twitter::REST::Client,如下所示

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']

我有一个(撬开)断点,在那里我运行了obj.client.user 15次(在不到1分钟的时间内-一直保持击中状态),然后我得到了rate_limit警告

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'

我认为twitter gem client.user会调用/1.1/users/show.json https://github.com/sferik/twitter/blob/master/lib/twitter/rest/users.rb#L257 吗?

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 ?

这意味着twitter api在15分钟的窗口中获得15个调用的限制速率限制?但是根据API文档 https://dev.twitter.com/docs/api/1.1/get/users/show 是180,而不是15.那么为什么15次调用后我会受到限制?

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?

当我使用Twitter API控制台 https://dev.twitter.com/console 时,我打电话给/1.1/get/users/show,它显示了180作为速率限制.因此,我不确定为什么twitter gem只能在15次通话后最终获得限制.

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.

反正还有一个地方,我可以在twitter gem中获得整个响应头,这样调试起来很容易阅读x-rate-limit-limitx-rate-limit-remaining

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

任何帮助将不胜感激.

推荐答案

此处是访问速率限制的更具体方法:

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')

然后您可以检查response哈希.

我希望这对某人有帮助.红宝石宝石的当前版本是5.14.0.

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

这篇关于Twitter api表示速率限制为180(通过用户身份验证),但使用twitter gem限制为仅15个调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆