我创建了这个代码,使用这个参考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)

我是基于这个create方法创建的参考: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

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.

Ruby相关问答推荐

Ruby:程序终止后仅写入/保存/可见的文件数据

为什么我的二维 Ruby 数组的多个值会发生变化,尽管只更改了其中一个?

Python 正则表达式是否等同于 Ruby 的原子分组?

在Ruby中将嵌套哈希键从CamelCase转换为snake_case

如何判断 Ruby 文件是否为空?

构造Ruby的现代方法是什么?

什么是 Sinatra/Rack 的非常简单的身份验证方案

判断文件是否包含字符串

检测安装的 CPU 数量

Ruby:从字节创建一个字符串

Ruby 中的 Object 和 BasicObject 有什么区别?

如何从最后一个元素开始遍历数组?

class_eval、class_exec、module_eval 和 module_exec 有什么区别?

Ruby 设计模式:如何制作可扩展的工厂类?

ActiveSupport::Memoizable 指的是哪种 Ruby memoize 模式?

Ruby 将 CSV 文件读取为 UTF-8 和/或将 ASCII-8Bit 编码转换为 UTF-8

在类方法中使用实例变量 - Ruby

不能在windows上安装thin

如何在没有 RVM 的 Ubuntu 上安装 Ruby 2

如何在 Ruby 中使用全局变量或常量值?