Google drive API V3,试图改变文档表和幻灯片以外文件的所有权



我正在使用google API for google drive, V3 with c# dot net。当试图将我的服务帐户的所有权更改为文档,表格和幻灯片(如。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 "";
        }

    }

使用这段代码,我可以上传所有文件,更改共享权限,但我不能更改所有权回到谷歌驱动器帐户

我终于找到了答案。我需要冒充另一个用户。
    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分钟的更改生效。

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

相关内容

  • 没有找到相关文章

最新更新