使用google-api-ruby-client从Google Drive下载文件
我尝试从Google Disk上的目录下载文件。使用google-api-ruby-client从Google Drive下载文件
的代码,其中大部分来自official quickstart guide复制,做工精细:
# ... code from official quickstart tutorial...
# Initialize the API
service = Google::Apis::DriveV3::DriveService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
# now the real deal
response = service.list_files(q: "'0ByJUN4GMe_2jODVJVVpmRE1VTDg' in parents and trashed != true",
page_size: 100,
fields: 'nextPageToken, files(id, name)')
puts 'Files:'
puts 'No files found' if response.files.empty?
response.files.each do |file|
puts "#{file.name} (#{file.id})"
# content = service.get_file(file.id, download_dest: StringIO.new)
end
输出看起来不错:
Files:
k.h264 (0B4D93ILRdf51Sk40UzBoYmZKMTQ)
output_file.h264 (0B4D93ILRdf51V1RGUDFIWFQ5cG8)
test.mp4 (0B4D93ILRdf51dWpoZWdBV3l4WjQ)
test2.mp4 (0B4D93ILRdf51ZmN4ZGlwZjBvR2M)
test3.mp4 (0B4D93ILRdf51ZXo0WnVfdVBjTlk)
12.mp4 (0ByJUN4GMe_2jRzEwS1FWTnVkX00)
01.mp4 (0ByJUN4GMe_2jSlRWVEw4a1gxa2s)
01.mp4 (0ByJUN4GMe_2jOFpPMW9YNjJuY2M)
但是,一旦我取消content = service.get_file(file.id, download_dest: StringIO.new)
,我得到了很多的错误:
Files:
k.h264 (0B4D93ILRdf51Sk40UzBoYmZKMTQ)
/Users/mvasin/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/google-api-client-0.9.15/lib/google/apis/core/http_command.rb:211:in `check_status': Invalid request (Google::Apis::ClientError)
[...lots of other 'from' stuff...]
from /Users/mvasin/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/google-api-client-0.9.15/generated/google/apis/drive_v3/service.rb:772:in `get_file'
from quickstart.rb:56:in `block in <main>'
from quickstart.rb:54:in `each'
from quickstart.rb:54:in `<main>'
但这是它应该按照ruby部分的方式"download files" official tutorial。
我也尝试过content = service.get_file(file.id, download_dest: "/tmp/#{file.name}")
,但是它失败的时候出现了相同的一组错误。
更新:这是我的发现。如果你开始Google Drive API Ruby quick start tutorial,并要做成下载的东西,
1)的变化范围,不只是让你的脚本读取meatadata,但读取文件内容,以及:
SCOPE = Google::Apis::DriveV3::AUTH_DRIVE_METADATA_READONLY
至少 SCOPE = Google::Apis::DriveV3::AUTH_DRIVE_READONLY
2)过滤谷歌文档文件,因为你不能以这种方式下载它们,你必须转换它们。要过滤器他们:
2.1)添加mime_type
设置的Fileds:
response = service.list_files(page_size: 10, fields: 'nextPageToken, files(id, name, mime_type)')
2.2),并在打印文件的ID和名称的最后循环,放像
service.get_file(file.id, download_dest: "/your/path/#{file.name}") unless file.mime_type.include? "application/vnd.google-apps"
从你得到的错误,它说你的要求是无效的。所以请确保你的请求是正确的。下面是关于如何下载使用Ruby文件documentation(只需点击红宝石的例子来查看Ruby代码。)
注意:下载文件要求用户至少有 读取权限。此外,您的应用必须获得允许读取文件内容的 scope 的授权。
欲了解更多信息,请查看这些线程:
我将范围扩大到'Google :: Apis :: DriveV3 :: AUTH_DRIVE',但它没有帮助。我仍然有同样的错误。 – Mike
http_command.rb:211看起来像这样:'raise Google :: Apis :: ClientError.new(message,status_code:status,header:header,body:body)'。我必须调试这些变量:'status'似乎是'403','header'中有''只能下载含有二进制内容的文件。使用Google Docs文件导出。“刚刚发生的是,我现在在文件夹中有一些Google文档文件,并且他们抛出了错误。一个非信息错误。现在可以了! – Mike
你得到不'download_dest同样的错误:'说法? – Nakilon
@Nakilon如果我省略'download_dest:...则不会出现错误...'和'content'变量似乎是'Google :: Apis :: DriveV3 :: File'类的适当实例。但是,我如何保存文件? 'content.web_content_link'给出'nil'(为了不过滤字段,我改变了'response = service.list_files(page_size:10)',但它没有帮助)。 – Mike