gpt4 book ai didi

CentOS 6.2 上的 Ruby OpenSSL::SSL::SSLError

转载 作者:行者123 更新时间:2023-12-04 19:32:14 24 4
gpt4 key购买 nike

我正在尝试在 CentOS 6.2 中运行以下代码(取自 codeacademy):

require 'rubygems'
require 'oauth'



# Change the following values to those provided on dev.twitter.com

# The consumer key identifies the application making the request.

# The access token identifies the user making the request.

consumer_key = OAuth::Consumer.new(

"MY_KEY",

"MY_SECRET")

access_token = OAuth::Token.new(

"STRING1",

"STRING2")


# All requests will be sent to this server.

baseurl = "https://api.twitter.com"



# The verify credentials endpoint returns a 200 status if

# the request is signed correctly.

address = URI("#{baseurl}/1.1/account/verify_credentials.json")



# Set up Net::HTTP to use SSL, which is required by Twitter.

http = Net::HTTP.new address.host, address.port

http.use_ssl = true

http.verify_mode = OpenSSL::SSL::VERIFY_PEER


# Build the request and authorize it with OAuth.

request = Net::HTTP::Get.new address.request_uri

request.oauth! http, consumer_key, access_token


# Issue the request and return the response.

http.start

response = http.request request

puts "The response status was #{response.code}"

并收到以下错误消息:

/usr/lib/ruby/1.8/net/http.rb:586:in `connect': SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)



key 已被省略(毕竟是 secret 的),但我使用的是正确的。
安装了必要的 gem。

问题可能是什么?

最佳答案

http = Net::HTTP.new address.host, address.port
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
...

您还需要:
http.ca_file = File.join(File.dirname(__FILE__), "ca-cert.pem")

自从它的高音:
$ openssl s_client -connect api.twitter.com:443
CONNECTED(00000003)
depth=1 C = US, O = "VeriSign, Inc.", OU = VeriSign Trust Network, OU = Terms of use at https://www.verisign.com/rpa (c)10, CN = VeriSign Class 3 Secure Server CA - G3
verify error:num=20:unable to get local issuer certificate
verify return:0
---
Certificate chain
0 s:/C=US/ST=California/L=San Francisco/O=Twitter, Inc./OU=Twitter Security/CN=api.twitter.com
i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)10/CN=VeriSign Class 3 Secure Server CA - G3
1 s:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=Terms of use at https://www.verisign.com/rpa (c)10/CN=VeriSign Class 3 Secure Server CA - G3
i:/C=US/O=VeriSign, Inc./OU=VeriSign Trust Network/OU=(c) 2006 VeriSign, Inc. - For authorized use only/CN=VeriSign Class 3 Public Primary Certification Authority - G5
...

您需要顶级颁发者( i: 在级别 1),即 VeriSign 3 类公共(public)主要证书颁发机构 - G5。您可以从 Public Root CA - VeriSign 获得。 .文件名是 PCA-3G5.pem .

下载根目录后,您可以运行 s_client再次,服务器证书将验证:
$ openssl s_client -connect api.twitter.com:443 -CAfile PCA-3G5.pem

关于CentOS 6.2 上的 Ruby OpenSSL::SSL::SSLError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23653671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com