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

标签 c# android google-drive-api

我尝试以编程方式使用 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/

相关文章:

android - 评分在 HTC 中显示不正确

android - 如果我们使用多个 APK,BuildConfig.VERSION_CODE 不会改变吗

java - "onRadioButtonClicked"方法不适用于由 "onCreate"方法实现的单选按钮

php - 可恢复上传到谷歌驱动器,但服务帐户错误

c# - 序列化一个通用类型的 FaultException

c# - ClosedXml 中的 "Cannot convert theme color to Color"

google-drive-api - Google Drive 中的协作者仍然可以访问已删除的文件。我怎样才能完全删除它?

android - 无法使用编程预选帐户启动 android google drive 应用程序

c# - WPF 和后台 worker 以及调用线程必须是 STA

c# - 如何在 Ocelot 中实现 cors header