0

C# App

I need to update a file that is shared in Gdrive using File ID.

Code:

public static Google.Apis.Drive.v2.Data.File UpdateFile(DriveService service, 
String fileId, String newTitle, String newDescription, String newMimeType, 
String newFilename, bool newRevision)
        {
        try
        {
            // First, retrieve the file from the Google Drive.
            Google.Apis.Drive.v2.Data.File file = service.Files.Get(fileId).Fetch();

            // Set the file's new metadata.
            file.Title = newTitle;
           // file.Description = newDescription;
            file.MimeType = newMimeType;

            // Get the file's new content and read it into a memory stream
            byte[] byteArray = System.IO.File.ReadAllBytes(newFilename);
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            // Call the Update API method passing in the updated information.
            FilesResource.UpdateMediaUpload request = service.Files.Update(file, fileId, stream, newMimeType);
            // Tell Google Drive if this is a new revision of the file or not.
            request.NewRevision = newRevision;
            // Execute the update
            request.Upload();

            // Get the response back from Google Drive and set the updatedFile 
            //object to the returned File informational object
            Google.Apis.Drive.v2.Data.File updatedFile = request.ResponseBody;
            // Return the updated file object so the caller has a handle on it.
            return updatedFile;
        }

Error thrown: File not found.

NomadTraveler
  • 1,086
  • 1
  • 12
  • 37

1 Answers1

0

2 things:

  1. Please update the library to its latest version. You can download the latest drive API from NuGet - http://www.nuget.org/packages/Google.Apis.Drive.v2/. Then you can use the latest Upload API which solves several bugs. Notice also that from 1.4.0-beta we changed the Fetch method to Execute, so you will need to change your code. Remember to install also Google.Apis.Authentication package (in the next release which scheduled to be several weeks from now, we are going to present a new Auth library which will make the OAuth2 "dance" much easier and will support other Windows platforms like WinRT and WP as well).

  2. Are you sure that the error is thrown from "request.Upload()" and not from "byte[] byteArray = System.IO.File.ReadAllBytes(newFilename);" or "service.Files.Get(fileId).Fetch();". Can you attach the exact exception and its stacktrace?

Eyal

peleyal
  • 3,472
  • 1
  • 14
  • 25