首先,您缺少身份验证代码。如果您使用的 apis.drive.v2 不起作用,您所指的教程也使用旧版本的库。这个适用于较新的库:My Tutorial Google Drive api C#(还有一个 winform 示例项目)
但是这里有一些例子来说明如何让它工作:
UserCredential credential;
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = "YourClientId", ClientSecret = "YourClientSecret" },
new[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile },
"user",
CancellationToken.None,
new FileDataStore("Drive.Auth.Store")).Result; }
上面的代码将为您提供用户凭据。它会弹出一个新的浏览器窗口,要求用户自动调整您的应用程序。 My Tutorial here
BaseClientService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample",
});
上面的代码允许您访问云端硬盘服务。看看我们如何将我们从 Oauth 调用中获得的凭证发送给它?然后上传它的调用 service.files.insert
// File's metadata.
File body = new File();
body.Title = "NewDirectory2";
body.Description = "Test Directory";
body.MimeType = "application/vnd.google-apps.folder";
body.Parents = new List<ParentReference>() { new ParentReference() { Id = "root" } };
try
{
FilesResource.InsertRequest request = service.Files.Insert(body);
request.Execute();
}
catch (Exception e)
{
Console.WriteLine("An error occurred: " + e.Message);
}