Undistraction
Undistraction

Reputation: 43402

Ruby Google Drive Authorization Error when Writing to a File

I'm using google_drive to download and manipulate Google Drive documents for seeding a Rails App. I can download spreadsheets as files and parse them without problems, however I want to save the spreadsheet locally.

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

As per the readme I'm using download_to_file, but encounter an Authorization Error:

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

What am I doing wrong? It seems that auth is failing, but I am already authorized and have downloaded the file data.

Upvotes: 0

Views: 541

Answers (1)

Undistraction
Undistraction

Reputation: 43402

It's a known issue: 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).

Upvotes: 1

Related Questions