Omniauth Facebook 错误 - Faraday::Error::ConnectionFailed

46

(FYI:我正在按照railscast#241的Twitter Omniauth进行操作。我已经成功使用了Twitter,现在要切换到Facebook)

一旦我使用Omniauth登录Facebook,就会出现这个错误:

Faraday::Error::ConnectionFailed
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed

这是什么意思?

这是我的代码

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, '<key from fb>', '<another key from fb>'
end

我的代码实际上并没有太多内容,我只有一个 sessionController,我想使用 to_yaml 来查看 request.env 中的内容。

class SessionsController < ApplicationController
    def create
        raise request.env["omniauth.auth"].to_yaml
    end
end

如何解决 Faraday 错误?

9个回答

63

我在Mac OS X Lion 10.7.4上使用以下解决方案解决了这个问题:

$ rvm remove 1.9.3 (or whatever version of ruby you are using)
$ rvm pkg install openssl
$ rvm install 1.9.3 --with-openssl-dir=$rvm_path/usr

在此之后,您需要下载缺失的 cacert.pem 文件:

$ cd $rvm_path/usr/ssl
$ sudo curl -O http://curl.haxx.se/ca/cacert.pem
$ sudo mv cacert.pem cert.pem

1
非常有用。在Rails、Devise和Google OAuth下,它在10.8.2上运行良好。 - Dimitris
1
重新安装 Ruby 可以解决编译步骤的问题。但是在运行“make”时出现以下错误:Error running 'make', please read /usr/local/rvm/log/ruby-1.9.3-p392/make.log There has been an error while running make. Halting the installation. - Caroline Schnapp
1
如下答案所示,您应该引用Rails项目中的此帖子:http://railsapps.github.io/openssl-certificate-verify-failed.html - mindtonic
当我在Ubuntu 13上运行时,看到了Beware, 'rvm pkg ...' is deprecated, read about the new autolibs feature: 'rvm help autolibs'.。如果使用rvm autolibs ...,你的有用指令会如何改变? - Kevin Meredith
我在2015年尝试过这个方法,最终解决了我的问题。http://railsapps.github.io/openssl-certificate-verify-failed.html#comment-657918573 - kittyminky
显示剩余2条评论

29
你遇到这个错误是因为Ruby找不到可信任的根证书。
Windows的解决方法:https://gist.github.com/867550 苹果/ Linux的解决方法:http://martinottenwaelter.fr/2010/12/ruby19-and-the-ssl-error/ <--该网站现已关闭。
以下是来自上述网站的苹果/ Linux解决方法:
解决方法是安装curl-ca-bundle端口,其中包含Firefox使用的相同根证书。
sudo port install curl-ca-bundle

并告诉您的 https 对象使用它:

https.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt'

请注意,如果您想在Ubuntu上运行您的代码,您需要设置ca_path属性,其默认证书位置为/etc/ssl/certs。

最终,这将适用于Mac OS X和Ubuntu:

require 'net/https'
https = Net::HTTP.new('encrypted.google.com', 443)
https.use_ssl = true
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
https.ca_path = '/etc/ssl/certs' if File.exists?('/etc/ssl/certs') # Ubuntu
https.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists('/opt/local/share/curl/curl-ca-bundle.crt') # Mac OS X
https.request_get('/')

1
谢谢,你的回答来得正是时候,因为我正在度过一周的假期。谢谢! - user1372829
那个网站一定刚刚挂了。我加了谷歌缓存版本。 - Neil Hoff
7
请问您需要的翻译是:"tell your https object to use it:" 的意思是什么?我需要详细了解上下文才能为您提供准确的翻译。至于代码的位置,通常情况下,您应该将代码放在Rails应用程序中的相关文件中,具体取决于您的应用程序结构和要实现的功能。 - GMA
@NeilHoff,你能详细解释一下GeorgMillo的问题吗? - daedelus_j
@GeorgeMillo,这对我有用,在openssl(ruby 2,rails 4)中导出环境变量:https://dev59.com/g2gv5IYBdhLWcg3wQ-qd - daedelus_j
你也可以使用 brew install curl-ca-bundle 进行安装。 - Schneems

21

Andrei的回答在我使用的Mac OSX 10.8.3上不能正常工作。我曾经重新安装openssl以安装ruby 2.0,自那时以来一直遇到此错误。感谢Andrei的回答和Rails项目中的说明,我已经修复了这个问题。

我运行了:

$ rvm -v
$ rvm get head
# Installation of latest version of rvm...
$ rvm -v
# rvm 1.19.5 (master)
$ rvm osx-ssl-certs status all
# Certificates for /usr/local/etc/openssl/cert.pem: Old.
# Certificates for /Users/mpapis/.sm/pkg/versions/openssl/0.9.8x/ssl/cert.pem: Old.
$ sudo rvm osx-ssl-certs update all
# Updating certificates...

然后我通过运行rvm osx-ssl-certs status all再次检查证书是否正确更新,但是/usr/local/etc/openssl/cert.pem仍未更新。我不知道这是否必要,但我接下来做了以下操作:

$ cd /usr/local/etc/openssl/
$ curl -O http://curl.haxx.se/ca/cacert.pem
$ mv cacert.pem cert.pem

问题已经得到解决。希望这能帮助遇到同样问题的其他人。


我已经将这个答案发送给了5个人,它总是解决了问题!你太厉害了! - 0bserver07

8

这对我很有用(在Mac OS X上):

$ brew install curl-ca-bundle
$ export SSL_CERT_FILE=/usr/local/opt/curl-ca-bundle/share/ca-bundle.crt

这个对我在OSx 10.8.5上也有效,这里的其他解决方案都没有用,只有这个可以,谢谢! - ramz15

3

3

1
对于Windows 7:Neil Hoff的解决方案链接(https://gist.github.com/867550)对我无效。以下是有效的方法:
使用cmd.exe:
curl -o c:\cacert.pem http://curl.haxx.se/ca/cacert.pem
set SSL_CERT_FILE=c:\cacert.pem

使用 msysgit bash:
curl -o /c/cacert.pem http://curl.haxx.se/ca/cacert.pem
export SSL_CERT_FILE=/c/cacert.pem

如果你的Windows 7命令行没有curl,请在这里获取它:http://www.confusedbycode.com/curl/#downloads 原始解决方案来自于这里 - 感谢:https://github.com/chef/chef-dk/issues/106 Dunn.

0

请查看认证宝石。描述:

确保net/https使用OpenSSL :: SSL :: VERIFY_PEER来验证SSL证书,并在OpenSSL找不到证书包的情况下提供证书包


0
Andrei的回答对我有用,但是当我尝试重新安装Ruby 1.9.3时遇到了一个巨大的障碍。因为我自从安装1.9.3以来已经安装了新版本的Xcode,所以在我打开Xcode首选项并从下载选项卡中安装命令行工具之前,我无法重新安装它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接