I'm trying to write some text into a new google doc file using the google-api-ruby-client.
I think the following should work, but it doesn't:
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')
When I run this I get Google::Apis::ClientError: badRequest: Bad Request
UPDATE: After Tanaike posted the correct answer below, I also had to move the file as it was created in the root directory of my service user. The final (working) code looks like this
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}")