0

我正在使用google_drive下載和操作Google Drive文檔以播種Rails應用程序。我可以下載電子表格作爲文件並解析它們,但是我想在本地保存電子表格。Ruby寫入文件時的Google Drive授權錯誤

require "google_drive" 

# Log in to Google Drive 
session = GoogleDrive.login("[email protected]", "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_filereadme,但遇到授權錯誤:

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

我在做什麼錯?看起來auth失敗了,但我已經被授權並下載了文件數據。

+0

我得到確切同樣的錯誤。 –

+0

@LasseDahlEbert這是一個已知的問題。請參閱:https://github.com/gimite/google-drive-ruby/issues/34 – Undistraction

+0

謝謝,對於單挑 - 我只搜索未解決的問題以查找線索。 –

回答

1

這是一個衆所周知的問題:https://github.com/gimite/google-drive-ruby/issues/34

download_to_file uses different API, and it seems it only supports downloading as HTML. To download as PDF, you need to write code to fetch the URL you mentioned manually. You can probably use session.request() method to reuse the authorization google-drive-ruby provides.

It would be nice to have that feature in google-drive-ruby. But the API google-drive-ruby uses (Document List API) will be deprecated, so I need to switch to Google Drive API. So I will implement that feature after the switch (if it's available in Google Drive API).