我想上传文件到谷歌驱动器。为此,我有一个启用了域范围权限的服务帐户。 “@xyx.com
”是我的域名。我有一个共同的“[email protected]
”谷歌驱动器。模仿用户电子邮件到谷歌服务帐户
Google服务帐号为“[email protected]
”。我需要将文件上传到“[email protected]
”。我试图模仿服务帐户的“[email protected]
”。
下面是我的代码
public static DriveService AuthenticateServiceAccount(string serviceAccountEmail, string keyFilePath)
{
// check the file exists
if (!File.Exists(keyFilePath))
{
return null;
}
//Google Drive scopes Documentation: https://developers.google.com/drive/web/scopes
string[] scopes = new string[] { DriveService.Scope.Drive, // view and manage your files and documents
DriveService.Scope.DriveAppdata, // view and manage its own configuration data
DriveService.Scope.DriveFile, // view and manage files created by this app
DriveService.Scope.DriveMetadata,
DriveService.Scope.DriveMetadataReadonly, // view metadata for files
DriveService.Scope.DrivePhotosReadonly,
DriveService.Scope.DriveReadonly, // view files and documents on your drive
DriveService.Scope.DriveScripts }; // modify your app scripts
var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
try
{
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scopes,
User = "[email protected]",
}.FromCertificate(certificate));
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "CIM_GD_UPLOAD",
});
return service;
}
catch (Exception ex)
{
throw ex;
}
}
我收到以下错误。
Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method.", Uri:""
我使用谷歌API V3。
请帮助我,是否可以模拟用户帐户到服务帐户?或引导我正确的方式上传/从谷歌驱动器检索文件。