【问题标题】:Google drive api Permissions.Delete doesn't workGoogle drive api Permissions.Delete 不起作用
【发布时间】:2019-03-23 00:17:02
【问题描述】:

我是我的谷歌驱动器的所有者,我可以使用以下代码打开“可共享链接”,但是当我想将其设置为私有时(基本上关闭可共享链接) 然后发生异常:

Google.GoogleApiException: 'Google.Apis.Requests.RequestError The owner of a file cannot be removed. [403] Errors [     Message[The owner of a file cannot be removed.] Location[ - ] Reason[cannotRemoveOwner] Domain[global] ]

我可以轻松设置文件的权限:

        var permission = new Google.Apis.Drive.v3.Data.Permission();
        permission.Role = "reader";
        permission.Type = "anyone";
        var aaaa = Authentication.service.Permissions.Create(permission, fileIdAction).ExecuteAsync();

这是我的代码(不起作用):

        //Get PermissionId
        var get = Authentication.service.About.Get();
        get.Fields = "*";
        var permissionId = get.Execute().User.PermissionId;

        //Delete method
        Authentication.service.Permissions.Delete(fileIdAction, permissionId).Execute();

我认为permissionId 可能是一个问题,所以我尝试了不同的方法来获取它,但是,它们都不起作用(例外 - 关于所有者)。就像在这个线程中一样 - How to get permissionId in Drive API v3?

关于如何将文件设置回私有的任何想法?感谢您的帮助。

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    答案就在问题中。 “无法删除文件的所有者。”

    您尝试删除的permissionId 是主要所有权权限,而不是您之前创建的共享权限。要获得第二个permissionId,请在创建时保存它(在响应中),或者get 文件的元数据并查看permissionIds[]

    【讨论】:

    • 感谢您的回复,现在说得通了。我通过使用developers.google.com/drive/api/v3/reference/files/get 发现了一个简单的解决方法,您可以在fields = * 中请求有关文件的所有信息,然后我看到具有两个值的permissionIds - 第一个是“anyoneWithLink”,第二个是长数字。我一直在使用第二个号码,但是,正如您所说的那样(号码 - 所有者)是错误的。所以我用“anyoneWithLink”替换了“permissionId”,它成功了!感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    相关资源
    最近更新 更多