我尝试调用Google Geocode API时出现SSL错误

时间:2015-03-19 01:37:14

标签: ruby google-maps ssl google-geocoder

我正在尝试使用Ruby打开此网址:https://maps.googleapis.com/maps/api/geocode/json?address=1600 Pennsylvania Avenue Northwest, Washington, DC 20500

当我在浏览器中打开URL时,它会起作用:Firefox v36.0.1。

但是当我在Ruby中尝试它时,我得到一个错误。

我的代码:

require 'open-uri'

baseURL = 'https://maps.googleapis.com/maps/api/geocode/json?address='
url = baseURL+'1600 Pennsylvania Avenue Northwest, Washington, DC 20500'
p url
p open(URI.encode(url))

回应:

C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `block in connect'
    from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
    from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `connect'
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:756:in `do_start'
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:776:in `buffer_open'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:678:in `open'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:33:in `open'

修改

当我在第一行之后添加require "openssl"; OpenSSL.debug=true时,我收到此错误:

OSSL_DEBUG: IS NOW ON!
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800: warning: error on stack: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed (OpenSSL::SSL::SSLError)
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `block in connect'
    from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:55:in `timeout'
    from C:/Ruby193/lib/ruby/1.9.1/timeout.rb:100:in `timeout'
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:800:in `connect'
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:756:in `do_start'
    from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:745:in `start'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:306:in `open_http'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:776:in `buffer_open'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:203:in `block in open_loop'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `catch'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:201:in `open_loop'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:146:in `open_uri'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:678:in `open'
    from C:/Ruby193/lib/ruby/1.9.1/open-uri.rb:33:in `open'

1 个答案:

答案 0 :(得分:1)

您可能需要更新Ruby的SSL证书 从Google搜索中获取文件“cacert.pem”并将其放在文件夹中:

C:\Ruby193\lib\ruby\1.9.1\rubygems\ssl_certs

(我试图从你发布的错误中猜出你的Ruby文件夹)

这可能是导致您的问题的原因

修改

还需要设置一个环境变量。 我将它放在我的开发和生产初始化程序中:

ENV["SSL_CERT_FILE"] = "C:/Ruby193/lib/ruby/1.9.1/rubygems/ssl_certs/cacert.pem"

这仅为Rails设置环境变量,避免必须使用Windows控制面板进行设置。