我正在尝试使用 Ruby 打开此 URL: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'
最佳答案
您可能必须更新 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 控制面板进行设置。
关于ruby - 当我尝试调用 Google Geocode API 时出现 SSL 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29135799/