Marine Grillot
Marine Grillot

Reputation: 23

Google Drive Api Error 403 cannotAddParent with service account

Using a service account, Google Drive API and Google SpreadSheet API, I create a spreadsheet that i then move to a specific folder, using the following code :

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;
    }
}

This used to work fine for a year or so, but since today, the MoveFileToFolder request throw the following exception:

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]
]

The weird thing is that if I create a new service account and use it instead of the previous one, it works fine again.

I've looked for info on this "cannotAddParent" error but I couldn't find anything.

Any ideas on why this error is thrown ?

Upvotes: 2

Views: 1155

Answers (1)

H4kor
H4kor

Reputation: 1562

I have the same problem and filed in issue in the Google Issue Tracker. This is intended behavior, unfortunately. You are no longer able to place a file in multiple parents as in your example. See the linked documentation for migration.

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

Upvotes: 3

Related Questions