0

我正在使用google_drive下载和操作 Google Drive 文档以播种 Rails 应用程序。我可以将电子表格下载为文件并毫无问题地解析它们,但是我想将电子表格保存在本地。

require "google_drive"

# Log in to Google Drive
session = GoogleDrive.login("my.email@address.com", "MyPassword")

spreadsheet = session.spreadsheet_by_title('SpreadsheetName')

# At this point spreadsheet exists and I can manipulate it freely

spreadsheet.download_to_file('spreadsheet_name.xls')

根据我正在使用的自述文件 download_to_file,但遇到授权错误:

/Users/*[me]*/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/google_drive-0.3.2/lib/google_drive/session.rb:429:in `request': Response code 401 for get https://docs.google.com/feeds/download/spreadsheets/Export?key=*[key]*: 

  <HTML> (GoogleDrive::AuthenticationError)
    <HEAD>
      <TITLE>Unauthorized</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
      <H1>Unauthorized</H1>
      <H2>Error 401</H2>
    </BODY>
  </HTML>

  from /Users/Me/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/google_drive-0.3.2/lib/google_drive/file.rb:153:in `download_to_io'
        from /Users/Me/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/google_drive-0.3.2/lib/google_drive/file.rb:114:in `block in download_to_file'
        from /Users/Me/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/google_drive-0.3.2/lib/google_drive/file.rb:113:in `open'
        from /Users/Me/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/google_drive-0.3.2/lib/google_drive/file.rb:113:in `download_to_file'
        from db/seeds/modules.rb:14:in `<main>' 

我究竟做错了什么?似乎身份验证失败,但我已经获得授权并下载了文件数据。

4

1 回答 1

1

这是一个已知问题:https ://github.com/gimite/google-drive-ruby/issues/34

download_to_file 使用不同的 API,似乎只支持以 HTML 格式下载。要以 PDF 格式下载,您需要编写代码来手动获取您提到的 URL。您可能可以使用 session.request() 方法来重用 google-drive-ruby 提供的授权。

在 google-drive-ruby 中拥有该功能会很好。但是 google-drive-ruby 使用的 API(文档列表 API)将被弃用,所以我需要切换到 Google Drive API。所以我将在切换后实现该功能(如果它在 Google Drive API 中可用)。

于 2013-01-07T10:23:34.700 回答