如何使用服务帐户从谷歌驱动器API下载文件? [英] How to download file from google drive api with service account?

查看:151
本文介绍了如何使用服务帐户从谷歌驱动器API下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,谷歌黑客!

我正在使用Drive Service应用并成功上传文件:

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 有很多属性,如下所示:
download_url

file has a lot of properties, like this: download_url

但是当我尝试打开在这个 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...

推荐答案

答案很简单 - 我们无法从 file 对象,我们必须发送另一个get请求,就像下面这样下载它:

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

这篇关于如何使用服务帐户从谷歌驱动器API下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆