AnApprentice AnApprentice - 3 months ago 26x
Ruby Question

How to download a file via the Google Drive API?

I'm working to download a file via the google drive API using the gem google-api-client.

x = Google::Apis::DriveV2
drive =
drive.authorization = auth

files = drive.list_files
files.items.each_with_index do |file, index|
url_to_index = { |k, v| v if k == 'text/plain' }
file_content = open(url_to_index["text/plain"]).read

The problem is file_content is returning the google login screen not the file in text/plain format. It appears that when my rails app opens the URL it does not have access to the text file.

What's the right way to enable my rails app to grab the file in the text format?



Stated in Download Files

Depending on the type of download you'd like to perform — a file, a Google Document, or a content link — you'll use one of the following URLs:

  • Download a file — files.get with alt=media file resource
  • Download and export a Google Doc — files.export
  • Link a user to a file — webContentLink from the file resource

Downloading the file requires the user to have at least read access. Additionally, your app must be authorized with a scope that allows reading of file content.

You may go through the documentation for more information and examples.