Google Drive API(.NET) - 将所有权从服务帐户转移给域用户 [英] Google Drive API (.NET) - Transfer ownership from Service Account to Domain user
问题描述
我使用 .NET客户端API 在Google驱动器上创建文件。使用服务帐户。
string [] scopes = new string [] {DriveService.ScopeDrive};
GoogleCredential凭证;
using(var stream = new FileStream(Directory.GetCurrentDirectory()+/Resources/GoogleCredentials.json,FileMode.Open,FileAccess.Read))
{
credential = GoogleCredential.FromStream (流).CreateScoped(作用域);
}
DriveService drive = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer =凭证,
});
我成功创建了文件,
var f = drive.Files;
var request = f.Create(new Google.Apis.Drive.v3.Data.File()
{
Name =Test from ASP.NET Core,
AppProperties = prop,
MimeType =application / vnd.google-apps.document
});
var file = await request.ExecuteAsync();
与所有域共享,但我无法将所有权转让给域用户。
权限权限=新权限()
{
EmailAddress =user@example.com,
Type =user,
Domain =example.com,
Role =owner
};
var requestpermission = drive.Permissions.Create(permission,file.Id);
requestpermission.TransferOwnership = true;
var perm = await requestpermission.ExecuteAsync();
我得到错误:
指定的域无效或不适用于给定的
权限类型。
我发现此链接,但不建议使用p12 cert文件。所以我想使用JSON。
t属于任何域。您最好选择创建一个服务帐户可以访问的团队云端硬盘,并执行两个阶段的过程:- 使用将文件移动到团队驱动器的服务帐户。
Files.update
并将addParents
参数设置为团队云端硬盘ID。 - 使用域用户将文件移出团队驱动器。
Files.update
与addParents
参数设置为root
(或某个目标文件夹的ID)以及设置为团队云端硬盘ID的removeParents
参数。
I am creating file on Google drive with .NET client API with Service account.
string[] scopes = new string[] { DriveService.Scope.Drive };
GoogleCredential credential;
using (var stream = new FileStream(Directory.GetCurrentDirectory() + "/Resources/GoogleCredentials.json", FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream).CreateScoped(scopes);
}
DriveService drive = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
I succesfully create file,
var f = drive.Files;
var request = f.Create(new Google.Apis.Drive.v3.Data.File()
{
Name = "Test from ASP.NET Core",
AppProperties = prop,
MimeType = "application/vnd.google-apps.document"
});
var file = await request.ExecuteAsync();
share it with all domain, but I can not transfer ownership to a domain user.
Permission permission = new Permission()
{
EmailAddress = "user@example.com",
Type = "user",
Domain = "example.com",
Role = "owner"
};
var requestpermission = drive.Permissions.Create(permission, file.Id);
requestpermission.TransferOwnership = true;
var perm = await requestpermission.ExecuteAsync();
I get error:
The specified domain is invalid or not applicable for the given permission type.
I found this link, but using p12 cert file is not recommended. So I want to use JSON.
Ownership transfers can only be done between users in the same domain, and service accounts don't belong to any domain. You're best option may be to create a Team Drive that the service account has access to, and perform a two stage process:
- Use the service account to move the file into the team drive.
Files.update
with theaddParents
parameter set to the Team Drive ID. - Use the domain user to move the file out of the team drive.
Files.update
with theaddParents
parameter set toroot
(or some target folder's ID) and theremoveParents
parameter set to the Team Drive ID.
这篇关于Google Drive API(.NET) - 将所有权从服务帐户转移给域用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!