Search code examples
pythongoogle-drive-apigoogle-drive-shared-drive

Google Drive create new folder in shared drive


I'm able to create a new folder within a parent folder on my personal Google Drive, but when I try to do it in a shared drive, I get this error:

<HttpError 404 when requesting https://www.googleapis.com/drive/v3/files?fields=id&alt=json returned "File not found:

It looks like a similar problem from this question, which wasn't resolved.

I'm a manager on the account, and other commands, such as creating new files, work fine.

This is the function I'm successfully using when writing to my personal Drive:

def create_folder_in_folder(folder_name,parent_folder_id):

    file_metadata = {
    'name' : folder_name,
    'parents' : [parent_folder_id],
    'mimeType' : 'application/vnd.google-apps.folder'
    }

    file = service.files().create(body=file_metadata,
                                    fields='id').execute()

    print ('Folder ID: %s' % file.get('id')) 

Solution

  • How about this modification?

    From:

    file = service.files().create(body=file_metadata, fields='id').execute()
    

    To:

    file = service.files().create(body=file_metadata, supportsAllDrives=True, fields='id').execute()
    
    • supportsAllDrives=True is added.
      • I think that the reason of the error message is due to this.

    Note:

    • In this case, it supposes that you have the permission for creating the folder to the shared Drive.

    Reference: