【问题标题】:Google Drive Api Error 403 cannotAddParent with service accountGoogle Drive Api Error 403 cannotAddParent with service account
【发布时间】:2020-10-07 10:55:03
【问题描述】:

使用服务帐户、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”错误的信息,但找不到任何信息。

关于为什么抛出此错误的任何想法?

【问题讨论】:

  • new File()
  • 我在尝试创建文件夹时遇到了与 Google Drive API 相同的问题。问题开始于一天前。最近没有动过代码。
  • H4kor 你能解释或分享更多关于你的代码的东西吗?对于 ,@DalmTo 提议的更改对您有用吗?

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


【解决方案1】:

我有同样的问题,并在 Google 问题跟踪器中提交了问题。不幸的是,这是预期的行为。您不再能够像示例中那样将文件放置在多个父级中。请参阅迁移的链接文档。

从 2020 年 9 月 30 日开始,您将无法再将文件放置在多个父文件夹中;每个文件必须只有一个父文件夹位置。相反,您可以结合使用状态检查和新的快捷方式实现来完成与文件相关的操作。

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

【讨论】:

    猜你喜欢
    • 2022-12-02
    • 2015-01-11
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    相关资源
    最近更新 更多