我使用sferik的宝石效果很好,除非我尝试使用代理。 (https://github.com/sferik/twitter/blob/master/examples/Configuration.md#using-a-proxy)
那是我的代码:
proxy = nil
if account.proxy_ip.present?
proxy = {
host: account.proxy_ip,
port: account.proxy_port
}
proxy[:username] = account.proxy_user
proxy[:password] = account.proxy_password
end
client = Twitter::REST::Client.new do |config|
config.consumer_key = account.consumer_key
config.consumer_secret = account.consumer_secret
config.access_token = account.access_token_key
config.access_token_secret = account.access_token_secret
config.proxy = proxy if proxy.present?
end
client.update('hello world')
这就是我得到的错误:
undefined method `host=' for nil:NilClass (NoMethodError)
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/options.rb:31:in `block in update'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/options.rb:20:in `each'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/options.rb:20:in `update'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/options.rb:7:in `from'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/options.rb:231:in `from'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/connection.rb:286:in `proxy'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday/connection.rb:83:in `initialize'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday.rb:70:in `new'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/faraday-0.9.2/lib/faraday.rb:70:in `new'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/client.rb:97:in `connection'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/request.rb:25:in `initialize'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/utils.rb:50:in `new'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/utils.rb:50:in `perform_request'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/utils.rb:95:in `perform_request_with_objects'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/utils.rb:87:in `perform_post_with_objects'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/users.rb:232:in `block in users'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/utils.rb:37:in `block in pmap'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/utils.rb:37:in `each_slice'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/utils.rb:37:in `each'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/utils.rb:37:in `collect'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/utils.rb:37:in `pmap'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/utils.rb:25:in `flat_pmap'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/users.rb:231:in `users'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/twitter-5.15.0/lib/twitter/rest/friends_and_followers.rb:112:in `block in follow'
正确设置代理IP和端口。我无法弄清楚问题是什么。任何线索如何克服这个?
由于
答案 0 :(得分:0)
所以我深入研究了faraday的连接库,twitter gem用它来建立与API的连接。事实证明,代理哈希不期望require("react-clipboard")
或host
属性。相反,它需要port
属性,该属性必须是uri
(Check out the code here)的实例
因此,如果您使用代理背后的Addressable::URI
,请按以下步骤声明:
Twitter::REST::Client
希望这会有所帮助。干杯!