gpt4 book ai didi

c# - Google Drive API 上传/创建文件 (.NET)

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:43 25 4
gpt4 key购买 nike

我正尝试按照提供的示例使用 Drive API here .我已经成功地下载和列出文件,但在创建目录和上传文件时遇到了一些问题。

当我尝试上传文件时(使用以下代码),没有出现异常但文件没有上传,当我调试消息时我得到的请求是这样的:

{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 的最新版本。

最佳答案

解决了:)

我必须做的几件事:

  1. 使用Zlib.Portable签名版dll

  2. 我的范围配置不正确

  3. 必须删除现有凭据并根据新范围重新登录

关于c# - Google Drive API 上传/创建文件 (.NET),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com