使用设计为facebook-omniauth配置代理

时间:2014-05-06 07:41:29

标签: ruby-on-rails facebook oauth devise

我为我的Rails 4.1应用omniauth autorization添加了宝石facebook-omniauthdevise。它在我的家用电脑上工作正常。但是当我尝试使用工作站中的omniauth时,proxy后面出现了以下错误:

I, [2014-05-06T11:20:52.161344 #9573]  INFO -- omniauth: (facebook) Request phase initiated.
I, [2014-05-06T11:20:52.332481 #9573]  INFO -- omniauth: (facebook) Request phase initiated.
I, [2014-05-06T11:20:52.511584 #9573]  INFO -- omniauth: (facebook) Callback phase initiated.
E, [2014-05-06T11:20:52.517301 #9573] ERROR -- omniauth: (facebook) Authentication failure! failed_to_connect: Faraday::ConnectionFailed, Network is unreachable - connect(2) for "graph.facebook.com" port 443

我试图在我的设计初始化程序中指定代理,但它不起作用:

config.omniauth :facebook, Rails.application.secrets.facebook_key, Rails.application.secrets.facebook_secret,
               {:client_options => { :proxy => "172.16.3.160:4226"} }

如何正确指定代理?

1 个答案:

答案 0 :(得分:1)

我找到了解决方案:

由于Omniauth使用法拉第,我们只需要为法拉第指定代理。 我们可以这样做,将此代码放在我们的omniauth.rb初始值设定项中:

  require 'socket'
  ip=Socket.ip_address_list.detect{|intf| intf.ipv4_private?}
  my_ip = ip.ip_address if ip

  if my_ip == "172.*.1*0.233"
    module Faraday
      class Connection
        alias_method :old_initialize, :initialize

        def initialize(url = nil, options = {})
          proxy = 'http://172.16.3.160:4226'
          (url.is_a?(Hash) ? url : options).merge!(proxy: proxy)
          old_initialize(url, options)
        end
      end
    end
  end