gpt4 book ai didi

c# - Google Drive API V3,试图更改文档表和幻灯片以外的文件的所有权

转载 作者:太空宇宙 更新时间:2023-11-03 12:41:03 24 4
gpt4 key购买 nike

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

错误:错误请求。用户消息:“您还不能更改此项目的所有者。(我们正在处理它。)。

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

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

    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 帐户。

最佳答案

我终于找到了答案。我需要冒充另一个用户。

    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 分钟让更改生效。

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

关于c# - Google Drive API V3,试图更改文档表和幻灯片以外的文件的所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38970938/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com