3
votes

I'm having trouble creating a folder in Google Drive using the google-drive-api library in ruby. I can successfully log in, list folders, even add a file to the root folder. I have a folder called 'Applications' under root folder. Listing all files show the id for this folder, lets call it "0BwiVxEBt'. In my controller I init the google drive connection successfully (as I'm able to list all files) and then call the create_parent('Test') method which is in my google_drive_connection.rb:

def create_folder (title)
file = @drive.files.insert.request_schema.new({
    'title' => title,
    'description' => title,
    'mimeType' => 'application/vnd.google-apps.folder'
})
file.parents = [{"id" => '0BwiVxEBt'}] # 0BwiVxEBt is id for Applications folder
media = Google::APIClient::UploadIO.new(title, 'application/vnd.google-apps.folder')
result = @client.execute(
        :api_method => @drive.files.insert,
        :body_object => file,
        :media => media,
        :parameters => {
            'uploadType' => 'multipart',
            #'convert' => false,
            'alt' => 'json'})
    if result.status == 200
        return result.data
    else
        puts "An error occurred: #{result.data['error']['message']}"
        return nil
    end
end

For what I saw in other post to insert a folder is just like a file insert (which works fine in ahother method in this rb file), a title with no extension, and the mimeType as 'application/vnd.google-apps.folder'

Error: No such file or directory - Test

Probably is a minor issue, but can't seem to find it!

Thanks for any pointer / help provided.

EDIT Found the problem, so for anyone interested in the solution, for folders you just need to send the metadata in the request, no need for media part, just needs body (metadata) information, so my 'create_folder' method results in:

def create_folder (title)
  file = @drive.files.insert.request_schema.new({
    'title' => title,
    'description' => title,
    'mimeType' => 'application/vnd.google-apps.folder'
  })
  file.parents = [{"id" => '0BwiVxEBt'}] # 0BwiVxEBt is id for Applications folder
  result = @client.execute(
        :api_method => @drive.files.insert,
        :body_object => file)
  if result.status == 200
    return result.data 
      else
    puts "An error occurred: #{result.data['error']['message']}"
    return nil
  end
end
1
Could you post the code where that error occurs, too?JunYoung Gwak
I'm calling this method from a controller action 'create' def create googleDriveCon = GoogleDriveConnection.new(emailAddress) googleDriveCon.create_folder('Test') end I modify the call to create_folder by a method called 'retrieve_all_files' which outputs all file names + ids and works perfectly fine. The error is thrown by the api, as I don't have any place to trigger that error messageAlexG

1 Answers

2
votes

Found the answer to my question, added edit in original question with solution. In few words, for a folder only add the body part, not the media part in the request. For folders, only metadata is required.