我正在尝试通过 API Google Drive 发送文件,但是,我找不到任何有关如何使用身份验证服务帐户执行 C# 上传文件的文档。
我下载了 Daimto 库,但是,当我们使用身份验证 ClientId 和 ClientSecret 时,他使用 DriveService 类上传。但是使用帐户服务的身份验证,他返回到 PlusService 类,发现无法通过这种方式上传文件。
有人能帮我吗?此致
使用身份验证服务帐户
public PlusService GoogleAuthenticationServiceAccount()
{
String serviceAccountEmail = "106842951064-6s4s95s9u62760louquqo9gu70ia3ev2@developer.gserviceaccount.com";
//var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { PlusService.Scope.PlusMe }
}.FromCertificate(certificate));
// Create the service.
var service = new PlusService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Plus API Sample",
});
return service;
}
使用身份验证 ClientId 和 ClientSecret
public DriveService GoogleAuthentication(string userClientId, string userSecret)
{
//Scopes for use with the Google Drive API
string[] scopes = new string[] { DriveService.Scope.Drive, DriveService.Scope.DriveFile };
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = userClientId, ClientSecret = userSecret }, scopes, Environment.UserName, CancellationToken.None, new FileDataStore("Daimto.GoogleDrive.Auth.Store")).Result;
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Drive API Sample"
});
return service;
}
Daimto 类方法,使文件上传到谷歌驱动器 DaimtoGoogleDriveHelper.uploadFile(_service, fileName, item.NomeArquivo, directoryId);
如您所见,Daimto 库有一个上传方法,但是使用了 _service 参数,即 DriveService 类型,即 GoogleAuthentication 方法返回的内容。但是 GoogleAuthenticationServiceAccount 方法返回的是 PlusService 类型,与 DriveService 类型不兼容。