带有服务帐号的 Google 云端硬盘对象的下载链接



我正在构建一个使用带有服务帐户的谷歌云端硬盘的 API。

Google 云端硬盘对象附带一个@web_view_link属性,如下所示:

"https://drive.google.com/file/d/0B9eyEerjltIgZ2dyUTB1eGNjYUk/view?usp=drivesdk"

当我使用个人帐户在浏览器中访问此@web_view_link时,该链接是有效的,可以从那里下载对象。

当我使用服务帐户@web_view_link粘贴到浏览器中时,Google云端硬盘会抱怨权限,即使我使用帐户所有者访问,我也无法访问它。

是否可以使用服务帐户从Google云端硬盘访问下载链接?

我正在使用google-drive-rubygem,这是检索该@web_view_link网址的代码:

def create_url
folder_name = Discourse.current_hostname
found = google_files.select { |f| f.id == id }
file_title = found.first.title
file_url = session.collection_by_title(folder_name).file_by_title(file_title).human_url
end

服务帐户不是您的个人帐户。服务帐户属于您的应用程序。确保您在生成服务帐号时已启用云端硬盘 API,并将角色设置为所有者或编辑者。

SO 帖子中的此片段也可能有助于在 ruby 中使用服务帐户下载:

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)
drive.get_file(file.id, download_dest: '/tmp/my_file.txt') 

最新更新