5

尝试通过 SSL 连接到 Imgur API 时出现错误。这是代码和错误:

  API_URI = URI.parse('https://api.imgur.com')
  API_PUBLIC_KEY = 'Client-ID --'

  ENDPOINTS = {
    :image   => '/3/image',
    :gallery => '/3/gallery'
  }

  # Public: Upload an image
  #
  # args    - The image path for the image to upload
  # 
  def upload(image_path)
    http = Net::HTTP.new(API_URI.host)
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    params   = {'image' => File.open(image_path)}
    request  = Net::HTTP::Post.new(API_URI.request_uri)
    request.set_form_data(params)
    request.add_field('Authorization', API_PUBLIC_KEY)

    response = http.request(request)
    puts response.body
  end

和错误:

`connect': SSL_connect returned=1 errno=0 state=SSLv2/v3 read server hello A: unknown protocol (OpenSSL::SSL::SSLError)

我知道 VERIFY_NODE 不是一个好习惯,但我现在只想测试连接。

红宝石版本:1.9.2

4

2 回答 2

17

创建 HTTP 客户端时指定端口可解决此问题。

http = Net::HTTP.new(API_URI.host, API_URI.port)

或者

http = Net::HTTP.new(API_URI.host, 443)
于 2013-04-07T06:30:33.597 回答
0

对我来说,这是因为我将服务器作为 http ( tcp:// ) 服务器而不是 https ( ssl:// ) 启动。

IE

bundle exec puma config.ru -b 'tcp://0.0.0.0:3456?key=/path/to/key.key&cert=/path/to/cert.crt'

代替:

bundle exec puma config.ru -b 'ssl://0.0.0.0:3456?key=/path/to/key.key&cert=/path/to/cert.crt'
于 2015-05-28T02:53:17.117 回答