Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 220
rs41 Avatar asked Dec 13 '25 06:12

rs41


1 Answers

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')
like image 179
rs41 Avatar answered Dec 16 '25 05:12

rs41



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!