omm118
omm118

Reputation: 315

Google Drive API Uploading/Creating file (.NET)

I'm trying to use the Drive API by following the samples provided here. I've managed to download and list files just fine, but I'm having some problems with creating directories and uploading files.

When I try to upload a file (using the following code), no exception is raised but the file is not uploaded and when I debug the message I get for the request is this:

{Google.Apis.Upload.ResumableUpload<Google.Apis.Drive.v2.Data.File>.ResumableUploadProgress}
BytesSent: 0
Exception: {"Value cannot be null.\r\nParameter name: baseUri"}
Status: Failed

The code:

...
  if (System.IO.File.Exists(FilePath))
    {
        Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
        body.Title = System.IO.Path.GetFileName(FilePath);
        body.Description = "File uploaded by Drive Test";
        body.MimeType = DriveManager.GetMimeType(FilePath);
        body.Parents = new List<ParentReference>() { new ParentReference() { Id = service.About.Get().Execute().RootFolderId } };

        byte[] FileBytes = System.IO.File.ReadAllBytes(FilePath);
        System.IO.MemoryStream stream = new System.IO.MemoryStream(FileBytes);
        try
        {
            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, body.MimeType);
            request.Upload();
            Google.Apis.Drive.v2.Data.File res = request.ResponseBody;
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
        }
    }
    else
....

And when I try to create a directory an exception is raised:

'request.Execute()' threw an exception of type 'System.IO.FileLoadException'
Data: {System.Collections.ListDictionaryInternal}
FileName: "Zlib.Portable, Version=1.11.0.0, Culture=neutral, PublicKeyToken=qwerty"
FusionLog: "=== Pre-bind state information ===\r\nLOG: DisplayName = Zlib.Portable, Version=1.11.0.0, Culture=neutral, PublicKeyToken=qwerty\n (Fully-specified)\r\nLOG: Appbase = file:///C:/Users/xyz/documents/visual studio 2015/Projects/ConsoleApplication1/ConsoleApplication1/bin/Debug/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : Google.Apis, Version=1.9.2.27817, Culture=neutral, PublicKeyToken=qwerty.\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: Using application configuration file: C:\\Users\\xyz\\documents\\visual studio 2015\\Projects\\ConsoleApplication1\\ConsoleApplication1\\bin\\Debug\\ConsoleApplication1.exe.Config\r\nLOG: Using host configuration file: \r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\config\\machine.config.\r\nLOG: Post-policy reference: Zlib.Portable, Version=1.11.0.0, Culture=neutral, PublicKeyToken=qwerty\r\nLOG: Attempting download of new URL file:///C:/User
s/xyz/documents/visual studio 2015/Projects/ConsoleApplication1/ConsoleApplication1/bin/Debug/Zlib.Portable.DLL.\r\nWRN: Comparing the assembly name resulted in the mismatch: PUBLIC KEY TOKEN\r\nERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.\r\n"
HResult: -2146234304
HelpLink: null
InnerException: null
Message: "Could not load file or assembly 'Zlib.Portable, Version=1.11.0.0, Culture=neutral, PublicKeyToken=431cba815f6a8b5b' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)"
Source: "Google.Apis"
StackTrace: "   at Google.Apis.Requests.ClientServiceRequest`1.Execute() in c:\\code\\github\\google-api-dotnet-client\\Tools\\Google.Apis.Release\\bin\\Release\\1.9.2\\default\\Src\\GoogleApis\\Apis\\Requests\\ClientServiceRequest.cs:line 93"
TargetSite: {TResponse Execute()}

For the code:

.....
    Google.Apis.Drive.v2.Data.File NewDirectory = null;
    Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
    body.Title = title;
    body.Description = "Descripiton";
    body.MimeType = "application/vnd.google-apps.folder";
    body.Parents = new List<ParentReference>() { new ParentReference() { Id = service.About.Get().Execute().RootFolderId } };
    try
    {
        FilesResource.InsertRequest request = service.Files.Insert(body);
        NewDirectory = request.Execute();
    }
....

It's probably something with Zlib.Portable? But I'm really not sure what as I have the latest version from nuget.

Upvotes: 2

Views: 1445

Answers (1)

omm118
omm118

Reputation: 315

Solved it :)

A few things that I had to do:

  1. Use the Signed version dll of Zlib.Portable

  2. My Scopes weren't properly configured

  3. Had to remove the existing credentials and log in again according to the new scopes

Upvotes: 2

Related Questions