How to download file from google drive api with se

2019-03-01 17:15发布

问题:

Hello google hackers!

I am using Drive Service app and uploaded file successfully like this:

require 'googleauth'
require 'google/apis/drive_v2'

Drive = Google::Apis::DriveV2

upload_source = "/User/my_user_name/hacking.txt"
drive = Drive::DriveService.new
# Drive::AUTH_DRIVE is equal to https://www.googleapis.com/auth/drive
drive.authorization = Google::Auth.get_application_default([Drive::AUTH_DRIVE])
file = drive.insert_file({title: 'hacking.txt'}, upload_source: upload_source)

file has a lot of properties, like this: download_url

But when I try to open this download_url in browser it shows me blank screen. Why I can't download it?

I guess, that may be there are permission problems? But the scope is correct, and uploading is successful...

回答1:

The answer is simple - we cannot download it from file object, we must send another get request, just download it like this:

drive.get_file(file.id, download_dest: '/tmp/my_file.txt')