我正在使用google API for google drive,V3使用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)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云端硬盘帐户。
答案 0 :(得分: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分钟才能使更改生效。
这是我一直在寻找的解决方法。