Net::HTTP 无法访问 Google 文档列表数据 API

发布于 2024-12-03 07:36:30 字数 2115 浏览 1 评论 0原文

我正在尝试使用 ruby​​ 中的 Net::HTTP 使用以下代码来使用 Google 文档列表数据 API 版本 3.0。

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>'

I am trying to consume the Google Docs List Data API Version 3.0 using Net::HTTP in ruby using the following code.

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"))

This works for

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

but not for

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

which throws the following error

/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>'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撞了怀 2024-12-10 07:36:30

使用 ClientLogin 执行身份验证时,您必须为 DocsList API 指定正确的服务名称。您正在使用用于电子表格 API 的 service=wise ,尝试使用用于 DocsList API 的 service=writely 。

服务名称列表位于 http://code.google.com/apis /gdata/faq.html#clientlogin

You have to specify the correct service name for the DocsList API when performing authentication using ClientLogin. You are using service=wise which is for the Spreadsheet API, try with service=writely for the DocsList API.

The list of service names is available at http://code.google.com/apis/gdata/faq.html#clientlogin

荒路情人 2024-12-10 07:36:30

您需要在请求中包含“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

You need to include "verify_mode" option in your request.
I changed your "get_feed" method and it works for me.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文