使用云端硬盘 API 创建空文件
Create empty file using Drive API
我创建此代码,使用此参考 https://developers.google.com/drive/api/quickstart/ruby
class Drive
def initialize
@drive_service = Google::Apis::DriveV3::DriveService.new
@drive_service.client_options.application_name = APPLICATION_NAME
@drive_service.authorization = authorize
end
def create_file
data = { 'name': "My new Sheet #{Time.now.strftime('%d/%m/%Y %H:%M')}",
'mimeType': 'application/vnd.google-apps.spreadsheet' }
@drive_service.create_file(data).execute
end
def share_file
"to be filled"
end
def list_files
response = @drive_service.list_files(page_size: 10, 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})"
end
end
end
方法list_files
效果很好,但是create_file
return我这个错误:
Traceback (most recent call last):
2: from quickstart.rb:79:in `<main>'
1: from quickstart.rb:60:in `create_file'
/home/vagrant/.rvm/gems/ruby-2.7.2/gems/google-api-client-0.53.0/generated/google/apis/drive_v3/service.rb:895:in `create_file': unknown keywords: :name, :mimeType (ArgumentError)
我是根据这个创建方法参考创建的:https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/DriveV3/DriveService.html#create_file-instance_method
但我仍然无法让它工作,可能是哪里出了问题?
我尝试将创建更改为:@drive_service.create_file(file_object = data).execute
When supplying hashes for request objects. If it is the last argument
to a method, some versions of Ruby will interpret the hash as keyword
arguments. To prevent this, appending an empty hash as an extra
parameter will avoid misinterpretation.
file = {id: '123', title: 'My document', labels: { starred: true }}
file = drive.create_file(file) # Raises ArgumentError: unknown keywords: id, title, labels
file = drive.create_file(file, {}) # Returns a Drive::File instance
在您的代码中,只需添加 {}
作为 create_file 的第二个参数。
发件人:
@drive_service.create_file(data)
收件人:
@drive_service.create_file(data, {})
让我知道这是否对你有效。
让我们比较一下你的代码,
data = { 'name': "My new Sheet ...
@drive_service.create_file(data).execute
对比documented method signature.
`create_file(file_object = nil, enforce_single_parent: nil, ignore_default_visibility: nil, include_permissions_for_view: nil, keep_revision_forever: nil, ocr_language: nil, supports_all_drives: nil, supports_team_drives: nil, use_content_as_indexable_text: nil, fields: nil, quota_user: nil, user_ip: nil, upload_source: nil, content_type: nil, options: nil) {|result, err| ... } ⇒ Google::Apis::DriveV3::File`
- file_object (Google::Apis::DriveV3::File) (defaults to: nil)
第一个参数应该是 Google::Apis::DriveV3::File
的实例,但您传递的是 Hash
.
我创建此代码,使用此参考 https://developers.google.com/drive/api/quickstart/ruby
class Drive
def initialize
@drive_service = Google::Apis::DriveV3::DriveService.new
@drive_service.client_options.application_name = APPLICATION_NAME
@drive_service.authorization = authorize
end
def create_file
data = { 'name': "My new Sheet #{Time.now.strftime('%d/%m/%Y %H:%M')}",
'mimeType': 'application/vnd.google-apps.spreadsheet' }
@drive_service.create_file(data).execute
end
def share_file
"to be filled"
end
def list_files
response = @drive_service.list_files(page_size: 10, 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})"
end
end
end
方法list_files
效果很好,但是create_file
return我这个错误:
Traceback (most recent call last):
2: from quickstart.rb:79:in `<main>'
1: from quickstart.rb:60:in `create_file'
/home/vagrant/.rvm/gems/ruby-2.7.2/gems/google-api-client-0.53.0/generated/google/apis/drive_v3/service.rb:895:in `create_file': unknown keywords: :name, :mimeType (ArgumentError)
我是根据这个创建方法参考创建的:https://googleapis.dev/ruby/google-api-client/latest/Google/Apis/DriveV3/DriveService.html#create_file-instance_method 但我仍然无法让它工作,可能是哪里出了问题?
我尝试将创建更改为:@drive_service.create_file(file_object = data).execute
When supplying hashes for request objects. If it is the last argument to a method, some versions of Ruby will interpret the hash as keyword arguments. To prevent this, appending an empty hash as an extra parameter will avoid misinterpretation.
file = {id: '123', title: 'My document', labels: { starred: true }} file = drive.create_file(file) # Raises ArgumentError: unknown keywords: id, title, labels file = drive.create_file(file, {}) # Returns a Drive::File instance
在您的代码中,只需添加 {}
作为 create_file 的第二个参数。
发件人:
@drive_service.create_file(data)
收件人:
@drive_service.create_file(data, {})
让我知道这是否对你有效。
让我们比较一下你的代码,
data = { 'name': "My new Sheet ...
@drive_service.create_file(data).execute
对比documented method signature.
`create_file(file_object = nil, enforce_single_parent: nil, ignore_default_visibility: nil, include_permissions_for_view: nil, keep_revision_forever: nil, ocr_language: nil, supports_all_drives: nil, supports_team_drives: nil, use_content_as_indexable_text: nil, fields: nil, quota_user: nil, user_ip: nil, upload_source: nil, content_type: nil, options: nil) {|result, err| ... } ⇒ Google::Apis::DriveV3::File`
- file_object (Google::Apis::DriveV3::File) (defaults to: nil)
第一个参数应该是 Google::Apis::DriveV3::File
的实例,但您传递的是 Hash
.