0

我正在尝试让 Facebook Graph API 使用 Koala 在 Rails 2.3.5 中工作。在我的开发环境(Mac)中一切正常,但在生产环境中失败(Ubuntu 8.04.1)。

测试代码:

cookies = {"__utma"=>"158657023.1470934917.1315425280.1317394623.1317406089.55", "28aac256bbbe09dcae1eb7086ae1c326"=>"c14d42fcd1a6700aa6198a2ecd7587fa", "fbsetting_28aac256bbbe09dcae1eb7086ae1c326"=>"{\"connectState\":1,\"oneLineStorySetting\":3,\"shortStorySetting\":3,\"inFacebook\":false}", "__utmb"=>"158657023.23.10.1317406089", "28aac256bbbe09dcae1eb7086ae1c326_session_key"=>"c76e09e8124a194e26a9c1e9.1-635077092", "28aac256bbbe09dcae1eb7086ae1c326_user"=>"635077092", "__utmc"=>"158657023", "28aac256bbbe09dcae1eb7086ae1c326_expires"=>"0", "__utmz"=>"158657023.1317406089.55.52.utmcsr=angel.co|utmccn=(referral)|utmcmd=referral|utmcct=/thingspotter", "fbs_265881436784759"=>"\"access_token=AAADx0ViXiHcBAFMuyXsGRdZB4GmmHyZBQl9h7Ymqi6z0kk3ko5jRJGmdWZCdJQm7dWg2nCyraaZCJuJ6iurPxDHL6bZCMzosZD&expires=0&secret=815a69d93434d26034ada92bb2e80ae9&session_key=858594415a361e318af23dd1.1-635077092&sig=55f7e932f8e68a7dad6ae35af1877b45&uid=635077092\"", "_DefaultAppFacebook_session"=>"BAh7CToPc2Vzc2lvbl9pZCIlYzZjZGU5ZmJlNGU0NTM2ZjExNmQ1ZGQ5ZTc3ZjhiZDUiCmZsYXNoSUM6J0FjdGlvbkNvbnRyb2xsZXI6OkZsYXNoOjpGbGFzaEhhc2h7AAY6CkB1c2VkewA6EF9jc3JmX3Rva2VuIjFnZllGbGFVK0hMdVdZcDZaZjhJY3VJWVlZY2dEakc4VCtpM0dWTktSbFFRPSILbGF5b3V0IgxkZXNrdG9w--195b2d14ce7593502ec6d161906ceb0c38ed4219", "28aac256bbbe09dcae1eb7086ae1c326_ss"=>"0854c086618a5e075e2d9a866ce03f82"}
@facebook_cookies = Koala::Facebook::OAuth.new.get_user_info_from_cookie(cookies)
oauth_access_token = @facebook_cookies["access_token"]
graph = Koala::Facebook::GraphAPI.new(oauth_access_token)
fb_user = graph.get_object('me')

产生错误消息:

OpenSSL::SSL::SSLError: certificate verify failed
    from /usr/lib/ruby/1.8/net/http.rb:586:in `connect'
    from /usr/lib/ruby/1.8/net/http.rb:586:in `connect'
    from /usr/lib/ruby/1.8/net/http.rb:553:in `do_start'
    from /usr/lib/ruby/1.8/net/http.rb:542:in `start'
    from /usr/lib/ruby/1.8/net/http.rb:1035:in `request'
    from /usr/lib/ruby/1.8/net/http.rb:772:in `get'
    from /usr/lib/ruby/gems/1.8/gems/faraday-0.7.4/lib/faraday/adapter/net_http.rb:49:in `call'
    from /usr/lib/ruby/gems/1.8/gems/faraday-0.7.4/lib/faraday/request/url_encoded.rb:14:in `call'
    from /usr/lib/ruby/gems/1.8/gems/faraday-0.7.4/lib/faraday/request/multipart.rb:13:in `call'
    from /usr/lib/ruby/gems/1.8/gems/faraday-0.7.4/lib/faraday/connection.rb:203:in `run_request'
    from /usr/lib/ruby/gems/1.8/gems/faraday-0.7.4/lib/faraday/connection.rb:85:in `get'
    from /usr/lib/ruby/gems/1.8/gems/koala-1.2.0/lib/koala/http_service.rb:49:in `send'
    from /usr/lib/ruby/gems/1.8/gems/koala-1.2.0/lib/koala/http_service.rb:49:in `make_request'
    from /usr/lib/ruby/gems/1.8/gems/koala-1.2.0/lib/koala.rb:131:in `make_request'
    from /usr/lib/ruby/gems/1.8/gems/koala-1.2.0/lib/koala.rb:57:in `api'
    from /usr/lib/ruby/gems/1.8/gems/koala-1.2.0/lib/koala/graph_api.rb:215:in `graph_call'
    from /usr/lib/ruby/gems/1.8/gems/koala-1.2.0/lib/koala/graph_api.rb:36:in `get_object'

很明显和SSL证书有关,所以我尝试配置考拉:

Koala.http_service.http_options = {
  :ssl => {
    :ca_file => '/etc/ssl/certs',
    :verify_mode => OpenSSL::SSL::VERIFY_PEER,
    :verify => true
  }
}

...并将http://curl.haxx.se/ca/cacert.pem下载到/etc/ssl/certs文件夹。虽然没有任何明显的区别。

相关的宝石是:

faraday (0.7.4)
koala (1.2.0)
rails (2.3.5)
ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]

我找到了相关的 SO 帖子,但没有完美匹配。

4

3 回答 3

2

我对 Rails 3.2、Ruby 1.9.3、Ubuntu 11.10 也有同样的问题

除了证书文件现在是 ca-certificates.crt 之外,TenJack 的答案有效,所以:

Koala::HTTPService.http_options[:ssl] = {:ca_file => '/etc/ssl/certs/ca-certificates.crt'}
于 2012-04-13T18:52:35.597 回答
0

我在浏览时找到了解决方案(逐字记录):

Tweek for ssl 错误(使用 koala gem 连接到 facebook 时)在 Ubuntu 中:打开文件“.rvm/gems/ruby-1.9.2-p136/gems/koala-1.0.0/lib/koala/http_services.rb”添加第 60 行周围的以下行:

http = create_http(server(options), private_request, options)
http.use_ssl = true if private_request
http.verify_mode = OpenSSL ::SSL::VERIFY_PEER
http.ca_path = '/etc/ssl/certs' if File.exists?('/etc/ssl/certs') # Ubuntu
http.ca_file = '/opt/local/share/curl/curl-ca-bundle.crt' if File.exists?    ('/opt/local/share/curl/curl-ca-bundle.crt') # Mac OS X 

(参考文章:http ://martinottenwaelter.fr/2010/12/ruby19-and-the-ssl-error/ )

如果证书似乎仍然丢失(错误不会消失),请将此证书:http ://curl.haxx.se/ca/cacert.pem 添加到 /etc/ssl/certs 文件夹,文件名为 cacert.pem。

于 2012-01-15T08:47:06.887 回答
0

我遇到了完全相同的问题(Rails 2.3.8、Ruby 1.8.7、Ubuntu 8.04),我可以让它工作的唯一方法是指定 ca-bundle.crt 文件的确切路径(http://certifie .com/ca-bundle/ca-bundle.crt.txt):

Koala::HTTPService.http_options[:ssl] = {:ca_file => '/etc/ssl/certs/ca-bundle.crt'}
于 2012-01-29T04:23:22.683 回答