Search code examples
javascriptgoogle-drive-apirhomobile

how to upload file to google drive using sdk in uploadtype=media


I am trying to upload file to google drive. as

token_res = Rho::AsyncHttp.post(
:url => "https://www.googleapis.com/upload/drive/v2/files?uploadType=media", :headers => {'Content-Type'=> 'image/jpg','Content-Transfer-Encoding'=> 'base64', 'Authorization'=> tokn_final ,'X-JavaScript-User-Agent'=> 'Google APIs Explorer' }, :body => image_path )

A file is uploaded, if i give string in body as :body=> 'some string...' But if i give the image_path in the body, it is not uploading the file, it only uploading the path of the file as string.

i dono how to upload the file, pls help me to upload.


Solution

  • You need to put the image as binary encoded to the body, not the image's path. If you are using the Ruby client lib, it's quite easier.

    drive = client.discovered_api('drive', 'v2')
    media = Google::APIClient::UploadIO.new(file_name, mime_type)
    result = client.execute(
      :api_method => drive.files.insert,
      :media => media,
      :parameters => {'uploadType' => 'media'}
    )