【发布时间】:2020-10-06 15:11:55
【问题描述】:
我正在尝试使用 google-api-ruby-client 将一些文本写入新的 google doc 文件。
我认为以下应该有效,但它没有:
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::Apis::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}")
【问题讨论】:
标签: ruby google-docs google-docs-api