Converting local CSV to Google Sheets format via Drive API v2

I’m struggling to transform a CSV file on my computer into a Google Sheets document using the Drive API v2. I’d love to do this in Python but I’m open to raw HTTP requests too.

Here’s what I’ve tried so far:

  1. Set the request body content-type to ‘application/vnd.google-apps.spreadsheet’ and the media_body content-type to ‘text/csv’. This gave me a 401 Bad Request error.

  2. Made both the request body and media_body content-types ‘application/vnd.google-apps.spreadsheet’. Got a 400 Bad Request error.

  3. Played around with leaving out certain properties or tweaking content types. Usually ended up with 400 errors or Drive not recognizing the file as a proper spreadsheet.

I’m pretty stuck at this point. Has anyone successfully done this before? What am I missing? Any tips or code snippets would be super helpful!

I’ve found success with a slightly different approach that might be worth trying. Instead of using the Drive API v2, consider upgrading to v3. It offers improved functionality and cleaner endpoints for file operations.

With Drive API v3, you can use the files.create method with the uploadType=multipart parameter. This allows you to send both the file metadata and content in a single request. Set the mimeType in the metadata to ‘application/vnd.google-apps.spreadsheet’ and the file content as the CSV data.

If you’re using Python, the google-auth and google-api-python-client libraries make this process quite straightforward. You’ll need to set up OAuth2 credentials and use a service account for authentication.

This method has been more reliable in my experience and doesn’t require the two-step process of uploading and then converting. It’s worth giving it a shot if you’re still encountering issues with v2.

hey emma, i’ve done this before! here’s a quick tip: try using the v3 API instead. it’s way easier. just use the files.create method with uploadType=multipart. set the mimeType to ‘application/vnd.google-apps.spreadsheet’ in the metadata. works like a charm for me. goodluck!

I’ve actually tackled this exact problem before, and I found a solution that works reliably. The key is to upload the CSV file first, then convert it to a Google Sheet in a separate step.

Here’s the basic workflow I use:

  1. Upload the CSV file to Drive as-is, using ‘text/csv’ as the MIME type.
  2. Once it’s uploaded, use the ‘files.copy’ method to create a new file, specifying the ‘application/vnd.google-apps.spreadsheet’ MIME type.

This two-step process has been much more stable for me than trying to do it all in one go. It also gives you more control over the process, as you can check if the initial upload was successful before attempting the conversion.

If you’re using Python, the google-auth and google-auth-oauthlib libraries make authentication pretty straightforward. Then you can use the googleapiclient library to interact with the Drive API.

Hope this helps point you in the right direction! Let me know if you need any more specific guidance.