Upload CSV to Google Drive Spreadsheet using Drive v2 API

Zuza picture Zuza · Oct 7, 2012 · Viewed 25.5k times · Source

How can I upload a local CSV file to Google Drive using the Drive API v2 so that the uploaded file is in the native Google Spreadsheet format. Preferably in Python, but a raw HTTP request will suffice.

What I tried:

  1. request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'text/csv'. --> 401 Bad Request

  2. request body content-type: 'application/vnd.google-apps.spreadsheet', media_body content-type: 'application/vnd.google-apps.spreadsheet'. --> 400 Bad Request

  3. ... (a couple of others such as leaving a property out and similar, usually got 400 or Drive didn't recognise it as a native spreadsheet)

Answer

Claudio Cherubino picture Claudio Cherubino · Oct 9, 2012

Your insert request should specify text/csv as the content-type. The trick to get the file converted is to add the ?convert=true query parameter to the request url:

https://developers.google.com/drive/v2/reference/files/insert