Uploading PDF and Excel files to Google Docs using C#

Hey everyone,

I’ve been working on a project to upload files to Google Docs using C# and ASP.NET. So far, I’ve managed to get text and Word files working perfectly. But I’m hitting a roadblock with PDF and Excel files.

Every time I try to upload these formats, I keep getting a bad request error. It’s driving me crazy! I’m using SSO for Google login, if that helps.

Has anyone here successfully uploaded PDFs or Excel files to Google Docs with C#? Any tips or tricks would be super helpful. I feel like I’m missing something obvious, but I can’t figure out what.

Thanks in advance for any help you can offer. I really appreciate it!

hey mate, i’ve run into this before. try using the google drive API instead of docs API for PDFs and excel. it’s a bit tricky but should work. also, double check your mime types - that can mess things up. good luck with your project!

As someone who’s worked extensively with Google APIs in C#, I can share a few insights that might help you out. One thing I learned the hard way is that Google Docs doesn’t directly support PDF and Excel uploads. Instead, you’ll need to use the Google Drive API for initial upload, then convert if needed.

For PDFs, upload them to Drive first, then use the Drive API to convert to Google Docs format. With Excel files, upload to Drive and then use the Sheets API to open or convert them.

A crucial step I often overlooked was setting the correct MIME types. Make sure you’re using ‘application/pdf’ for PDFs and ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’ for Excel.

Also, check your OAuth scopes. You might need to include ‘https://www.googleapis.com/auth/drive’ for full access. Don’t forget to handle any potential conversion errors in your code.

Lastly, if you’re still having issues, try enabling verbose logging. It helped me pinpoint some non-obvious errors in my implementation. Good luck with your project!

I’ve dealt with a similar issue in the past. The problem likely stems from how Google Docs handles different file formats. For PDFs and Excel files, you might need to use Google Drive API instead of Docs API.

Try converting your PDFs to Google Docs format first using the Drive API’s files.create method with the ‘convert’ parameter set to true. For Excel files, you’ll want to use the Sheets API.

Also, double-check your MIME types. Make sure you’re using ‘application/pdf’ for PDFs and ‘application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’ for Excel files.

Lastly, ensure your OAuth scopes include ‘https://www.googleapis.com/auth/drive’ for full Drive access. This should cover both Docs and Sheets operations.

Hope this helps point you in the right direction!