Google 驱动器 Api 错误 403 cannotAddParent with service account
Google Drive Api Error 403 cannotAddParent with service account
使用服务帐户,Google Drive API 和 Google SpreadSheet API,我创建了一个电子表格,然后移动到特定文件夹,使用以下方法代码:
public async Task<File> SaveNewSpreadsheet(Spreadsheet spreadsheet, File folder)
{
try
{
Spreadsheet savedSpreadsheet = await _sheetService.Spreadsheets.Create(spreadsheet).ExecuteAsync();
string spreadsheetId = GetSpreadsheetId(savedSpreadsheet);
File spreadsheetFile = await GetFileById(spreadsheetId);
File spreadsheetFileMoved = await MoveFileToFolder(spreadsheetFile, folder);
return spreadsheetFileMoved;
}
catch (Exception e)
{
_logger.LogError(e, $"An error has occured during new spreadsheet save to Google drive API");
throw;
}
}
public async Task<File> MoveFileToFolder(File file, File folder)
{
try
{
var updateRequest = _driveService.Files.Update(new File(), file.Id);
updateRequest.AddParents = folder.Id;
if (file.Parents != null)
{
string previousParents = String.Join(",", file.Parents);
updateRequest.RemoveParents = previousParents;
}
file = await updateRequest.ExecuteAsync();
return file;
}
catch (Exception e)
{
_logger.LogError(e, $"An error has occured during file moving to folder.");
throw;
}
}
这在过去一年左右都可以正常工作,但从今天开始,MoveFileToFolder 请求抛出以下异常:
Google.GoogleApiException: Google.Apis.Requests.RequestError
Increasing the number of parents is not allowed [403]
Errors [
Message[Increasing the number of parents is not allowed] Location[ - ] Reason[cannotAddParent] Domain[global]
]
奇怪的是,如果我创建一个新的服务帐户并使用它而不是以前的服务帐户,它又可以正常工作了。
我已查找有关此“cannotAddParent”错误的信息,但找不到任何信息。
关于为什么抛出这个错误有什么想法吗?
我有同样的问题并在 Google 问题跟踪器中提交了问题。不幸的是,这是有意为之的行为。您不能再像示例中那样将文件放置在多个父项中。请参阅迁移的链接文档。
Beginning Sept. 30, 2020, you will no longer be able to place a file in multiple parent folders; every file must have exactly one parent folder location. Instead, you can use a combination of status checks and a new shortcut implementation to accomplish file-related operations.
使用服务帐户,Google Drive API 和 Google SpreadSheet API,我创建了一个电子表格,然后移动到特定文件夹,使用以下方法代码:
public async Task<File> SaveNewSpreadsheet(Spreadsheet spreadsheet, File folder)
{
try
{
Spreadsheet savedSpreadsheet = await _sheetService.Spreadsheets.Create(spreadsheet).ExecuteAsync();
string spreadsheetId = GetSpreadsheetId(savedSpreadsheet);
File spreadsheetFile = await GetFileById(spreadsheetId);
File spreadsheetFileMoved = await MoveFileToFolder(spreadsheetFile, folder);
return spreadsheetFileMoved;
}
catch (Exception e)
{
_logger.LogError(e, $"An error has occured during new spreadsheet save to Google drive API");
throw;
}
}
public async Task<File> MoveFileToFolder(File file, File folder)
{
try
{
var updateRequest = _driveService.Files.Update(new File(), file.Id);
updateRequest.AddParents = folder.Id;
if (file.Parents != null)
{
string previousParents = String.Join(",", file.Parents);
updateRequest.RemoveParents = previousParents;
}
file = await updateRequest.ExecuteAsync();
return file;
}
catch (Exception e)
{
_logger.LogError(e, $"An error has occured during file moving to folder.");
throw;
}
}
这在过去一年左右都可以正常工作,但从今天开始,MoveFileToFolder 请求抛出以下异常:
Google.GoogleApiException: Google.Apis.Requests.RequestError
Increasing the number of parents is not allowed [403]
Errors [
Message[Increasing the number of parents is not allowed] Location[ - ] Reason[cannotAddParent] Domain[global]
]
奇怪的是,如果我创建一个新的服务帐户并使用它而不是以前的服务帐户,它又可以正常工作了。
我已查找有关此“cannotAddParent”错误的信息,但找不到任何信息。
关于为什么抛出这个错误有什么想法吗?
我有同样的问题并在 Google 问题跟踪器中提交了问题。不幸的是,这是有意为之的行为。您不能再像示例中那样将文件放置在多个父项中。请参阅迁移的链接文档。
Beginning Sept. 30, 2020, you will no longer be able to place a file in multiple parent folders; every file must have exactly one parent folder location. Instead, you can use a combination of status checks and a new shortcut implementation to accomplish file-related operations.