1

I am trying to upload a file to Google Drive and share/access the same via Google Docs. I have copied below the code snippet. I am using Google Drive API V3. It's failing with error GoogleApiException:

Google.Apis.Requests.RequestError Bad Request [400].

Not sure what's missing in the request. I have tried few additional parameters of File, but it always returns Bad Request Error. If anyone know the solution please let me know.

string fileType = "application/vnd.google-apps.document";
var newFile = new File
      {
          Name = title,
          MimeType = fileType,
          Description= title,          
      };

var uploadProgress = DriveService.Files.Create(newFile, fileStream, fileType);
Google.Apis.Upload.IUploadProgress progress =  uploadProgress.Upload();
J.F.
  • 13,927
  • 9
  • 27
  • 65
mansoor
  • 11
  • 1

1 Answers1

0

I believe that in v3 you do not need to specify the mimeType as a third parameter for DriveService.Files.Create()

  • This is opposed to Files.Insert() in v2, where you would also need to specify Convert = true

  • Change to: var uploadProgress = DriveService.Files.Create(newFile, fileStream);

  • Make sure that your original mimeType is compatible with Google Docs

  • Make sure that you build fileStream correctly.

ziganotschka
  • 25,866
  • 2
  • 16
  • 33
  • Thank you for the info ziganotschka. The documentation is bit misleading, it show as if this is supported in V3. https://developers.google.com/drive/api/v3/manage-uploads#java_1 – mansoor Nov 23 '20 at 19:14
  • The documentation you linked contains the line `File file = driveService.files().create(fileMetadata, mediaContent)` which is equivalent to what I tried to explain: That it - the original mimeType goes into the mediaContent and the corresponding Google mimeType is specified in `fileMetadata` - so no need to specify it a second time by passing a third parameter to `Create()` – ziganotschka Nov 23 '20 at 19:28
  • I have been trying for the solution in C3 as follows. The API does require the third parameter fileType="application/vnd.google-apps.document" var newFile = new File { Name = title, MimeType = fileType, }; var uploadProgress = DriveService.Files.Create(newFile, fileStream, fileType).Upload(); – mansoor Nov 23 '20 at 20:03
  • My C# code is as follows. I tried options to give text/plain in File mime type and Create method, but always got back Bad Request 400 error. fileType="application/vnd.google-apps.document" ; var newFile = new File { Name = title, MimeType = fileType, }; var uploadProgress = DriveService.Files.Create(newFile, fileStream, fileType).Upload(); – mansoor Nov 23 '20 at 20:09
  • Actually what i am trying to do is Upload a file to google drive (which we already have in existing application) and be able to access the same files via Google Docs API so that we can leverage the feature of Docs like "Suggestions", but got error when i tried to access file using collaboration id or file Id. Please let me know if there is a way to access files uploaded using Google Drive API (.doc or .docx) via Google Docs API. – mansoor Nov 23 '20 at 20:14
  • Now I am confused. Are you trying to upload a file from your local Drive to your Google Drive (this is what your code suggests) or are you trying to access a Google Docs file that already exists on your Drive with Docs API? – ziganotschka Nov 23 '20 at 20:34
  • I want to upload a file to Google Drive (existing functionality in my application) using drive API, and from other parts of the application get the same file using Google Docs API, so that i can utilize the features of Docs like "Suggestions" ...etc. We have existing code to download from using Drive API, but we trying to see if it's possible to get it using Docs API – mansoor Nov 23 '20 at 20:44
  • If your file is in excel format (.doc or .docx) you cannot access it with the Docs API - you need to convert it first. The Drive API v3 method `Create()` performs the conversion automatically, as long as you specified the original mimeType in `fileStream` and desired mimeType in `newFile` correctly. If you still struggle - try the v2 method `Insert()` where you explicitly set `Convert` to `true`. [Here](https://stackoverflow.com/questions/34582098/upload-an-image-to-google-drive-with-ocr-and-convert-to-doc) is a good sample. – ziganotschka Nov 24 '20 at 07:53
  • And [here](https://stackoverflow.com/questions/12765813/upload-csv-to-google-drive-spreadsheet-using-drive-v2-api/42824117#42824117) yu can also see a sample for v3 - with 2 parameters. – ziganotschka Nov 24 '20 at 07:53
  • Thank you so much. I will try this example – mansoor Nov 24 '20 at 14:44