Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用google api ruby客户端将文本写入google文档文件_Ruby_Google Docs_Google Docs Api - Fatal编程技术网

使用google api ruby客户端将文本写入google文档文件

使用google api ruby客户端将文本写入google文档文件,ruby,google-docs,google-docs-api,Ruby,Google Docs,Google Docs Api,我正试图使用这个工具将一些文本写入一个新的google文档文件中 我认为以下方法应该有效,但不能: file_metadata = { name: "Testing", mime_type: 'application/vnd.google-apps.document' } @drive.create_file(file_metadata, fields: 'id', upload_sourc

我正试图使用这个工具将一些文本写入一个新的google文档文件中

我认为以下方法应该有效,但不能:

file_metadata = {
  name: "Testing",
  mime_type: 'application/vnd.google-apps.document'
}

@drive.create_file(file_metadata,
                   fields: 'id',
                   upload_source: StringIO.new("tt"),
                   content_type: 'text/text')
当我运行这个程序时,我得到了
Google::api::ClientError:badRequest:Bad Request


更新:在Tanaike发布了下面的正确答案之后,我还必须移动文件,因为它是在我的服务用户的根目录中创建的。最终(工作)代码如下所示

file_metadata = {
  name: "Filename",
  mime_type: 'application/vnd.google-apps.document'
}

file = @drive.create_file(file_metadata,
                   fields: 'id,parents',
                   upload_source: StringIO.new("Text to go in file"),
                   content_type: 'text/plain')

current_parent = file.parents.first
@drive.update_file(file.id, nil,
                   add_parents: "#{@folder_id}",
                   remove_parents: "#{current_parent}")

这次修改怎么样?在这种情况下,文本的mimeType是
text/plain

发件人: 致: 注:
  • 在我的环境中,当我使用
    content\u type:'text/text'
    时,也会发生相同的错误。当我使用
    content\u type:'text/plain'
    时,包含
    tt
    文本的Google文档将被创建到Google Drive上的根文件夹中
  • 在这种情况下,它假定您的
    @驱动器可以用于上载文件
参考:
content_type: 'text/text'
content_type: 'text/plain'