2011-09-08 79 views
0

我想要使用下面的代码在ruby中使用Net :: HTTP使用Google文档列表数据API版本3.0。Net :: HTTP无法访问Google文档列表数据API

require 'net/http' 

spreadsheets_uri = 'http://spreadsheets.google.com/feeds/spreadsheets/private/full' 
docs_uri = "https://docs.google.com/feeds/default/private/full?v=3" 

def get_feed(uri, headers=nil) 
    uri = URI.parse(uri) 
    Net::HTTP.start(uri.host, uri.port) do |http| 
    return http.get(uri.path, headers) 
    end 
end 

def get_headers(service) 
    http = Net::HTTP.new('www.google.com', 443) 
    http.use_ssl = true 
    path = '/accounts/ClientLogin' 
    data = "accountType=HOSTED_OR_GOOGLE&Email=#{EM}&Passwd=#{PW}&service=#{service}" 
    headers = { 'Content-Type' => 'application/x-www-form-urlencoded'} 
    resp, data = http.post(path, data, headers) 

    headers["Authorization"] = "GoogleLogin auth=#{data[/Auth=(.*)/, 1]}" 
    headers["GData-Version"] = "3.0" 
    headers 
end 

puts get_feed(spreadsheets_uri, get_headers("wise")) 
puts get_feed(docs_uri, get_headers("writely")) 

这适用于

'http://spreadsheets.google.com/feeds/spreadsheets/private/full' 

而不是

"https://docs.google.com/feeds/default/private/full?v=3" 

这引发以下错误

/usr/local/lib/ruby/1.9.1/net/protocol.rb:135:in `read_nonblock': end of file reached (EOFError) 


from /usr/local/lib/ruby/1.9.1/net/protocol.rb:135:in `rbuf_fill' 
    from /usr/local/lib/ruby/1.9.1/net/protocol.rb:116:in `readuntil' 
    from /usr/local/lib/ruby/1.9.1/net/protocol.rb:126:in `readline' 
    from /usr/local/lib/ruby/1.9.1/net/http.rb:2219:in `read_status_line' 
    from /usr/local/lib/ruby/1.9.1/net/http.rb:2208:in `read_new' 
    from /usr/local/lib/ruby/1.9.1/net/http.rb:1191:in `transport_request' 
    from /usr/local/lib/ruby/1.9.1/net/http.rb:1177:in `request' 
    from /usr/local/lib/ruby/1.9.1/net/http.rb:888:in `get' 
    from ./gd.rb:9:in `block in get_feed' 
    from /usr/local/lib/ruby/1.9.1/net/http.rb:627:in `start' 
    from /usr/local/lib/ruby/1.9.1/net/http.rb:490:in `start' 
    from ./gd.rb:8:in `get_feed' 
    from ./gd.rb:29:in `<main>' 

回答

1

您必须为已超出DocsList指定正确的服务名称API执行身份验证时使用ClientLogin的诱导。您正在使用service = wise,这是针对Spreadsheet API的,请尝试使用service = write的DocsList API。

服务名称的列表,请http://code.google.com/apis/gdata/faq.html#clientlogin

+0

谢谢!这似乎是正确的答案。不幸的是,我现在无法测试它,因为谷歌正在抛出一个令牌无效的错误 - 即使对于之前工作过的电子表格的以上未修改的代码。 – Peder

+0

不!虽然你的答案似乎解决了我的代码中的问题,但我仍然遇到了同样的错误。我在问题中更新了我的代码。 – Peder

1

您需要在您的要求“verify_mode”选项。 我改变了你的“get_feed”方法,它适用于我。

def get_feed(uri, headers=nil) 
    uri = URI.parse(uri) 
    https = Net::HTTP.new(uri.host, uri.port) 
    https.use_ssl = true 
    https.verify_mode = OpenSSL::SSL::VERIFY_NONE 

    https.start do |http| 
    return http.get(uri.request_uri, headers) 
    end 
end