google-drive-apigoogle-drive-shared-drivepydrive

How can I access a Team Drive instead of personal Google Drive using PyDrive?


I am trying to use PyDrive to programmatically list and then download files stored on a Team Drive that I can access in a browser. When I do this:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
drive = GoogleDrive(gauth)

file_list = drive.ListFile(
    {
        'q': "'root' in parents and trashed=false",
        'corpora': "teamDrive",
        'teamDriveId': <team drive ID>,
        'includeTeamDriveItems': "true",
        'supportsTeamDrives': "true"
    }
).GetList()
for file1 in file_list:
    print('title: %s, id: %s' % (file1['title'], file1['id']))

I only get the list of files on my personal drive, not the team drive. What am I doing wrong?

Note1: I got the <team drive ID> by opening the Team Drive (the root folder) in the browser and copying everything after "https://drive.google.com/drive/folders/"

Note2: Browser pops up at drive.ListFile and gives Authentication successful.


Solution

  • In a team drive, files can only have one parent folder and a team drive file's parent is never root.

    I was able to use the drive id again instead of root to get this to work, but I also used boolean values for true instead of strings:

    drive.ListFile({'q':"'<Team_Drive_Id_Or_Folder_Id>' in parents and trashed=false", 'corpora': 'teamDrive', 'teamDriveId': '<Team_Drive_Id>', 'includeTeamDriveItems': True, 'supportsTeamDrives': True}).GetList()