【问题标题】:Google drive API V3, trying to change the ownership of files other than docs sheets and slides谷歌驱动 API V3,试图更改文档表和幻灯片以外的文件的所有权
【发布时间】:2016-08-16 09:14:38
【问题描述】:

我正在使用用于谷歌驱动器的谷歌 API,带有 C# dot net 的 V3。当试图将所有权从我的服务帐户更改为“常规”驱动器帐户(因此它位于同一域内)时,除了文档、工作表和幻灯片(如 .zip 甚至 .pdf)之外的文件(如 .zip 甚至 .pdf)我收到一条错误消息:

Error: Bad Request. User message: "You can't yet change the owner of this item. (We're working on it.).

我想这与存储配额中未考虑文档、工作表和幻灯片这一事实有关。 (1)这有解决方法吗? (在上传之前尝试将文件名更改为 .doc 会导致文件的自动文件转换,之后就没有用了)。 (2) 这是否也发生在付费账户上? (3) 谷歌团队真的如错误消息中所说的那样“正在努力”吗?

更新: 这是我正在使用的代码:

    public string UploadFileToDrive(string FilePath, string ParentID)
    {
        try
        {
            Google.Apis.Drive.v3.Data.File body = new Google.Apis.Drive.v3.Data.File();
            string fileNameNoPath = System.IO.Path.GetFileName(FilePath);
            body.Name = "NewFile.ASC"; // some file names such as zip are not acceptable by google drive api
            //body.MimeType = GoogleDriveMimeTypes.GetGenericMimeTypeString();
            if (ParentID != null)
            {
                body.Parents = new List<string>();
                body.Parents.Add(ParentID);
            }


            byte[] byteArray = System.IO.File.ReadAllBytes(FilePath);
            System.IO.MemoryStream Ustream = new System.IO.MemoryStream(byteArray);

            var requestU = _CurrentDriveService.Files.Create(body, Ustream, "");
            requestU.Upload();
            var uploadedFileID = requestU.ResponseBody.Id;

            body.Name = fileNameNoPath;
            //body.MimeType = GoogleDriveMimeTypes.GetGenericMimeTypeString();
            FilesResource.CopyRequest cr = new FilesResource.CopyRequest(_CurrentDriveService, body, uploadedFileID);
            var newFile = cr.Execute();
            var NewFileNameID = newFile.Id;

            DeleteFileFromDrive(uploadedFileID);



            {
                Permission p = new Permission();
                p.Role = "reader";
                p.Type = "anyone";
                PermissionsResource.CreateRequest cc = new PermissionsResource.CreateRequest(_CurrentDriveService, p, NewFileNameID);
                cc.Execute();
            }


            // you can comment out the next block if using Auth client
            // 
            {
                // make main account the owner in order to take its size quota in main account not google service.
                Permission p = new Permission();
                p.Role = "owner";
                p.Type = "user";
                p.EmailAddress = "vizfilesender@gmail.com";


                PermissionsResource.CreateRequest cc = new PermissionsResource.CreateRequest(_CurrentDriveService, p, NewFileNameID);
                cc.TransferOwnership = true; // acknowledge transfer of ownership - must be set to "true" in order for role to change to "owner"
                cc.Execute();
            }





            return NewFileNameID;

        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine(e.Message);
            return "";
        }



    }

使用此代码,我可以上传所有文件,更改共享权限,但我无法将所有权更改回 Google Drive 帐户。

【问题讨论】:

  • 你能展示你如何改变权限的代码吗?此外,您可能想先检查此thread。它声明您使用Permissions: update 来完成这项工作。
  • 添加了一些代码。如果在@noogui 链接上找不到答案,则会参考该论坛。这就是我在这里发帖的原因。

标签: c# google-api google-drive-api


【解决方案1】:

我终于找到了答案。我需要冒充其他用户。

    var initializer = new ServiceAccountCredential.Initializer("blablablabla@blabla.iam.gserviceaccount.com")
    {
        Scopes = scope,
        User = "emailToImpersonate@domain"
    };

    var credential = new ServiceAccountCredential(initializer.FromPrivateKey("-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n"));

    var driveService = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName
    });

另外,请确保您授予 google 服务域范围的委托,如下所示: https://developers.google.com/drive/v2/web/delegation 并等待最多 10 分钟以使更改生效。

这是我一直在寻找的解决方法。

【讨论】:

  • 你是如何让它发挥作用的?执行相同的结果:Additional information: Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method.", Uri:""。创建了一个启用 DWD 的服务帐户并等待了 10 多分钟。没办法:(
  • 它确实对我有用。我假设您为您的应用程序创建了权限并将您的私钥字符串放在这里:“-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----\n”(其中由于明显的原因,我没有包括在内)?
  • 现在可以了,我们必须让 G Suite 管理员允许服务帐户模拟 Google Drive 范围。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-26
相关资源
最近更新 更多