0

次のコードを使用して、Ruby で Net::HTTP を使用して Google Docs List Data 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>'
4

2 に答える 2

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
于 2012-04-08T17:06:31.450 に答える
1

ClientLogin を使用して認証を実行する場合、DocsList API の正しいサービス名を指定する必要があります。スプレッドシート API 用の service=wise を使用しています。DocsList API 用の service=writely を試してください。

サービス名のリストは、http://code.google.com/apis/gdata/faq.html#clientloginで入手できます。

于 2011-09-09T17:56:01.977 に答える