Google Drive API上传/创建文件(. net)
本文关键字:文件 net 创建 Drive API 上传 Google | 更新日期: 2023-09-27 18:08:09
我试着按照这里提供的示例使用驱动器API。我已经成功地下载并列出了文件,但是我在创建目录和上传文件时遇到了一些问题。
当我尝试上传文件(使用以下代码)时,不会引发异常,但文件未上传,当我调试请求的消息时,我得到的消息是:
{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的Signed版本dll。便携式
-
我的scope没有正确配置
-
必须删除现有凭据并根据新的范围重新登录