2013-04-05 97 views
5

嘗試通過SSL連接到Imgur API會給我一個錯誤。下面的代碼和錯誤:HTTP POST(未知協議)上的SSL錯誤

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是不好的做法,但我只是想測試一下現在的連接。

Ruby版本:1.9.2

回答

15

創建HTTP客戶端時指定端口修復了此問題。

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

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

對我來說,那是因爲我已經開始將服務器作爲HTTP(TCP://)服務器,而不是HTTPS(SSL ://)。即代替

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' 
相關問題