2012-07-06 35 views
2

下面的curl命令按預期工作:
curl 'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=myappid&client_secret=myclientsecret'紅寶石HTTP庫獲取連接重置與Facebook應用程序

我想做同樣的事情在我的Ruby程序。下面的代碼給我一個錯誤:

fb_access_token_url = URI.parse(            
    'https://graph.facebook.com/oauth/access_token' +       
    '?grant_type=client_credentials' +          
    '&client_id=' + FACEBOOK_APP_ID +           
    '&client_secret=' + FACEBOOK_APP_SECRET)         
fb_access_token = Net::HTTP.get(fb_access_token_url)       

所以這段代碼:

fb_access_token_host = 'graph.facebook.com'         
fb_access_token_path_and_params = (           
    '/oauth/access_token' +             
    '?grant_type=client_credentials' +          
    '&client_id=' + FACEBOOK_APP_ID +           
    '&client_secret=' + FACEBOOK_APP_SECRET)         
https_port = Net::HTTP.https_default_port()         

fb_access_token = Net::HTTP.get_response(fb_access_token_host,    
             fb_access_token_path_and_params,  
             https_port) 

錯誤如下:

Errno::ECONNRESET: Connection reset by peer 
/usr/lib/ruby/1.8/net/protocol.rb:135:in `sysread' 
/usr/lib/ruby/1.8/net/protocol.rb:135:in `rbuf_fill' 
/usr/lib/ruby/1.8/timeout.rb:67:in `timeout' 
/usr/lib/ruby/1.8/timeout.rb:101:in `timeout' 
/usr/lib/ruby/1.8/net/protocol.rb:134:in `rbuf_fill' 
/usr/lib/ruby/1.8/net/protocol.rb:116:in `readuntil' 
/usr/lib/ruby/1.8/net/protocol.rb:126:in `readline' 
/usr/lib/ruby/1.8/net/http.rb:2028:in `read_status_line' 
/usr/lib/ruby/1.8/net/http.rb:2017:in `read_new' 
/usr/lib/ruby/1.8/net/http.rb:1051:in `request' 
/usr/lib/ruby/1.8/net/http.rb:948:in `request_get' 
/usr/lib/ruby/1.8/net/http.rb:380:in `get_response' 
/usr/lib/ruby/1.8/net/http.rb:543:in `start' 
/usr/lib/ruby/1.8/net/http.rb:379:in `get_response' 
/usr/lib/ruby/1.8/net/http.rb:356:in `get' 
./test-login-and-registration.rb:182:in `test_facebook_register' 

我使用Ruby 1.8.7在Ubuntu 12.04

+1

請看看這個問題的答案: http://stackoverflow.com/questions/9336884/getting-a-connection-reset-by-peer-error- when-hitting-google-contacts-api – 2012-07-17 09:45:19

+0

當我嘗試時,它找不到use_ssl =方法。我使用的是1.8.7,而不是1.9.3。 – 2012-07-17 14:24:40

+0

哦,我需要net/http而不是net/https。 – 2012-07-17 15:17:44

回答

3

感謝Manoj Awasthi讓我意識到有無法使用的ssl方法。

問題是我需要net/http而不是net/https。

工作片段:

fb_access_token_url = URI.parse(            
    'https://graph.facebook.com/oauth/access_token' +       
    '?grant_type=client_credentials' +          
    '&client_id=' + FACEBOOK_APP_ID +           
    '&client_secret=' + FACEBOOK_APP_SECRET)         
https = Net::HTTP.new(fb_access_token_url.host, fb_access_token_url.port)  
https.use_ssl = true               
https.verify_mode = OpenSSL::SSL::VERIFY_NONE         
fb_access_token = https.request_get(fb_access_token_url.path + '?' +   
            fb_access_token_url.query)