gpt4 book ai didi

c# - Google Drive API/SDK,使用服务帐户更改所有者失败

转载 作者:太空宇宙 更新时间:2023-11-03 16:01:44 25 4
gpt4 key购买 nike

我尝试以编程方式使用 google drive api v2 更新 google drive 中某些文件的所有者权限。(就像 Google 管理控制台在“Drive/Transfer Ownership”表单上所做的那样)我创建了一个服务帐户并将其 ID 与 super 管理员帐户结合使用来创建凭据和“DriveService”对象。我能够列出与我共享的所有文件并枚举"file"的“所有者”集合。向文件添加新权限 (role="writer") 工作正常。如果我尝试更改角色 ="owner"的新权限,我会收到内部错误 500。

以前有没有人见过这个错误,我该如何解决?

这是我的代码:

// ---------------------- BUILD CREDENTIALS -------------------------------
Google.Apis.Auth.OAuth2.ServiceAccountCredential m_Creds = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(<MY_SERVICE_ACCOUNT>)
{
Scopes = new[]
{
DriveService.Scope.Drive,
DriveService.Scope.DriveFile,
DriveService.Scope.DriveAppdata
}
,
User = <MY_ADMIN_ACCOUNT>
}.FromCertificate(certificate));

// ---------------------- BUILD CLIENTINIT -------------------------------
var myCInit = new BaseClientService.Initializer
{
ApplicationName = "Testapplication",
HttpClientInitializer = m_Creds
};

string myAccountname = "Test User1"; // accountname to look for
string myNewAccountEmail = "Test.User2@mydomain.com"; // email of new owner account

// ---------------------- DRIVE SERVIVE CREATION -------------------------------
var myDService = new DriveService(myCInit);

// ----------------------- GET THE FILE LIST------------------------------------
var myFList = myDService.Files.List().Execute();

foreach (Google.Apis.Drive.v2.Data.File currF in myFList.Items)
{

foreach (Google.Apis.Drive.v2.Data.User currUser in currF.Owners)
{
if (currUser.DisplayName.StartsWith(myAccountname))
{
// create new permission for new owner
Permission p = new Permission();
p.Role = "writer";
p.Type = "user";
p.Value = myNewAccountEmail;
myDService.Permissions.Insert(p, currF.Id).Execute(); // works fine, creates new permission

// get permission id
var myNewID = myDService.Permissions.GetIdForEmail(myNewAccountEmail).Execute();

// get my newly created permission
p = myDService.Permissions.Get(currF.Id, myNewID.Id).Execute();
p.Role = "owner";

// create update request
var myUpdR = myDService.Permissions.Update(p, currF.Id,myNewID.Id);
myUpdR.TransferOwnership = true; // yes, we want to be an owner

myUpdR.Execute(); // this call gets an "internal error 500"
}
}
}

感谢任何帮助。

谢谢。 TL

最佳答案

您只能在域内更改所有者。

关于c# - Google Drive API/SDK,使用服务帐户更改所有者失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21025820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com