使用google api ruby客户端将文本写入google文档文件
我正试图使用这个工具将一些文本写入一个新的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
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'
文本的Google文档将被创建到Google Drive上的根文件夹中tt
- 在这种情况下,它假定您的
@驱动器可以用于上载文件
content_type: 'text/text'
content_type: 'text/plain'