【发布时间】: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