Google Drive API上传/创建文件(.NET)
问题描述:
我试图按照提供的示例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。
答
解决它:)
几件事情,我不得不这样做:
使用Zlib.Portable的签名版本的DLL
我的作用域不正确配置
不得不删除现有凭证并根据新的范围再次登录
在每个受影响的项目中使用Zlib.Portable的签名版本dll –