Question

I have omniauth autorization for my Rails 4.1 app with gems facebook-omniauth and devise. It works fine on my home PC. But when I try to use omniauth from my workstation, which is behind the proxy I have the following error:

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

I have tried to specify proxy in my devise initializer, but it doesn't working:

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

How can I specify proxy properly?

Was it helpful?

Solution

I have found the solution:

Since Omniauth uses Faraday, we just need to specify proxy for Faraday. We can do it, putting this code in our omniauth.rb initializer:

  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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top