2015-08-15 198 views
2

我試圖按照提供的示例here使用Drive API。我已經設法下載和列出文件,但我在創建目錄和上傳文件時遇到了一些問題。Google Drive API上傳/創建文件(.NET)

當我嘗試上傳一個文件(使用下面的代碼),沒有異常被提出,但該文件沒有上傳,當我調試,我得到的請求的消息是這樣的:

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

的代碼:

... 
    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 
.... 

當我嘗試創建一個目錄將引發異常:

'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()} 

代碼:

..... 
    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(); 
    } 
.... 

這可能是Zlib.Portable?但我真的不確定,因爲我有最新版本的nuget。

回答

2

解決它:)

幾件事情,我不得不這樣做:

  1. 使用Zlib.Portable的簽名版本的DLL

  2. 我的作用域不正確配置

  3. 不得不刪除現有憑證並根據新的範圍再次登錄

+0

在每個受影響的項目中使用Zlib.Portable的簽名版本dll –