当前位置: 首页 > 知识库问答 >
问题:

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

涂羽
2023-03-14

我尝试使用google drive api v2以编程方式更新google drive中一些文件的所有者权限。(就像google Admin Console在“drive/Transfer Hownership”表单上所做的那样)我创建了一个服务帐户,并使用它的ID和超级管理帐户来创建凭据和“DriveService”对象。我可以列出所有与我共享的文件,并枚举“文件”的“所有者”集合。向文件添加一个新权限(role=“writer”)很好。如果我试图将新权限更改为role=“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"
                    }
                }
            }

共有1个答案

令狐宜民
2023-03-14

只能在域内更改所有者。

 类似资料:
  • 我正在尝试使用服务帐户访问目录API(https://developers.google.com/admin-sdk/Directory/v1/reference/users/list)。最简单的任务是列出组织中的用户。通过OAuth2.0 PlayGorund测试,这在我的用户帐户中运行良好。但我需要使用服务帐户。我正在阅读关于双腿OAuth(https://developers.google.

  • 是否可以使用服务帐户访问Google Admin Report SDK? 我有一个非常基本的例子,我正在尝试运行,我总是得到一个400错误返回。我已经验证了密钥和服务ID是正确的,我甚至已经将权限委托给这个服务帐户。难道这就是不可能吗?有人有什么想法吗? 返回的错误如下: {“代码”:401,“错误”:[{“domain”:“global”,“location”:“authorization”,“

  • 为Google Apps域的管理员寻找当前修改其他用户拥有的Google Drive对象权限的最佳方法。我将在Google App Engine上开发该应用程序。 使用两条腿的OAuth和Google文档列表API非常简单:https://developers.google.com/gdata/docs/auth/oauth#2LeggedOAuth但是现在Google Drive SDK是实时A

  • 我的团队目前正在开发一个应用程序,使用Admin SDK在GCP中列出公司的域用户,用于入职和非入职目的。 我们使用一个服务帐户来完成这个任务,并且在Google Admin's advanced settings中添加了 作用域。管理SDK API被激活,我们可以在凭据区域中看到服务帐户。 当我们使用参数 和 调用https://www.googleapis.com/admin/director

  • 目标是在G-Suite平台中建立一个服务帐户,应用程序可以使用该帐户执行操作,而无需提示用户进行身份验证。 我们遇到的问题与下面的帖子非常相似,但略有不同。 只要我们遵循“模拟”流程,事情就会正常进行。使用模拟,授权规则遵循被模拟的用户。理想情况下,我们希望相应地作用于服务帐户,而不是模拟用户。当我们这样做时,我们经常收到403。 我们已经尽力按照这里的指示去做了: 我们的Java片段与此类似:

  • 在Android应用程序的服务帐户授权以及Appengine中的servlet for Google service Account for Drive API中,无法获得页面令牌列表的更改,它返回一个空列表。但是https://developers.google.com/drive/v3/reference/changes/list相同的pageToken返回列表。 注意:如果使用谷歌授权代码流