Search code examples
ruby-on-railsrubymechanize-ruby

SSLv3 read server certificate error while using Mechanize to log into to flicker.com


I am trying to execute following code in Windows 7 machine using Mechanize and Ruby.

require 'mechanize'

a = Mechanize.new { |agent|
  # Flickr refreshes after login
  agent.follow_meta_refresh = true
}

a.get('https://www.flickr.com/') do |home_page|
  signin_page = a.click(home_page.link_with(:text => /Sign In/))
  puts signin_page.uri

  my_page = signin_page.form_with(:id => 'mbr-login-form') do |form|
    username_field = form.field_with(:id => 'login-username')
    username_field.value = 'some_username' 
    password_field = form.field_with(:id => 'login-passwd')
    password_field.value = 'some_password' 
  end.submit
end

I get following error message,

C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent/ssl_reuse.rb:70:in `connect': SSL_connect
SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent/ssl_reuse.rb:70:in `block in
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent/ssl_reuse.rb:70:in `connect'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:756:in `do_start'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/net/http.rb:751:in `start'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:700:in `start'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:631:in `connection_for'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/net-http-persistent-2.9.4/lib/net/http/persistent.rb:994:in `request'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mechanize-2.7.3/lib/mechanize/http/agent.rb:259:in `fetch'
        from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/mechanize-2.7.3/lib/mechanize.rb:440:in `get'
        from first.rb:19:in `<main>'

I installed gem 'Certified' but still getting error. Any insight?


Solution

  • Looks like most of the companies are moving away from using SSLv3, I had to tell mechanize to explicitly use ssl_version='TLSv1' to solve the problem.

    a = Mechanize.new {|a| a.ssl_version, a.verify_mode = 'TLSv1',OpenSSL::SSL::VERIFY_NONE}
    

    Thanks for the insight provided by this post.