How to download file from google drive api with service account?

有些话、适合烂在心里 提交于 2019-12-01 08:50:42

问题


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


来源:https://stackoverflow.com/questions/31874609/how-to-download-file-from-google-drive-api-with-service-account

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!