asp.net-core - Google Drive Api 错误 403 无法使用服务帐户添加父级

标签 asp.net-core google-drive-api google-sheets-api google-api-dotnet-client service-accounts

使用服务帐户、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.

https://developers.google.com/drive/api/v2/multi-parenting

关于asp.net-core - Google Drive Api 错误 403 无法使用服务帐户添加父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64242599/

相关文章:

c# - "There was an error trying to log you in: ' ' "Blazor WebAssembly 使用 IdentiyServer 身份验证

python - 无法访问谷歌电子表格

c# - 通过 Azure Web 应用程序连接到 Google Sheets

c# - 具有继承的 EF Core Fluent API 配置

c# - 在处理模板 api/[TodoController] 时,找不到 token TodoController 的替换值

c# - 为什么此字符串没有绑定(bind)到 ASP.NET .Core API 操作中的文件名参数?

google-drive-api - 阻止 Google Drive File Stream 立即同步已删除的文件

javascript - 谷歌脚本 : How to highlight a group of words?

ajax - 捕获 API 响应并将其上传到云端硬盘

google-sheets - 如何在 Google Sheets 中使用 .gs 脚本打印工作表/范围?