简体   繁体   中英

How to create a folder in Team Drive with Google's Python Drive Client API?

I don't seem to find many articles on this. The tutorial from Google only shows creating folders in a regular Google Drive folder.

Below is my function and it fails with oogleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "File not found: myteamdrivefolderid.". Details: "File not found: myteamdrivefolderid."> oogleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files?alt=json returned "File not found: myteamdrivefolderid.". Details: "File not found: myteamdrivefolderid.">

My app is a Python desktop app and has already been authorized to have full Drive read/write scope.

def create_folder(service, name, parent_id=None, **kwargs):
    # Create a folder on Drive, returns the newely created folders ID
    body = {
        'name': name,
        'mimeType': "application/vnd.google-apps.folder"
    }
    if parent_id:
        body['parents'] = [parent_id]
    if 'teamDriveId' in kwargs:
        body['teamDriveId'] = kwargs['teamDriveId']
    folder = service.files().create(body=body).execute()
    return folder['id']

I believe your goal and situation as follows.

  • You want to create new folder in the shared Drive using googleapis for python.
  • You have the permission for creating new folder in the shared Drive.
  • You have already been able to use Drive API.

Modification points:

  • In this case, please add supportsAllDrives to the query parameter.
  • It seems that teamDriveId is deprecated. Please use driveId . Ref

When above points are reflected to your script, it becomes as follows.

Modified script:

body = {
    'name': name,
    'mimeType': "application/vnd.google-apps.folder"
}
if parent_id:
    body['parents'] = [parent_id]
if 'teamDriveId' in kwargs:
    body['driveId'] = kwargs['teamDriveId'] # Modified
folder = service.files().create(body=body, supportsAllDrives=True).execute() # Modified
return folder['id']

Reference:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM