使用驱动器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返回此错误:

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

让我们比较一下您的代码,

data = { 'name': "My new Sheet ...
@drive_service.create_file(data).execute

与。记录的方法签名。

`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

根据本文档:

为请求对象提供哈希时。如果是最后一个论点对于一个方法,Ruby的某些版本会将散列解释为关键字论点。为了防止这种情况,请附加一个空散列作为额外散列参数将避免误解。

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, {})

如果这对你有用,请告诉我。

相关内容

  • 没有找到相关文章

最新更新