I am trying to retrieve all files in Google Drive, but only those in 'My Drive'. I tried including "'me' in owners" in the query, but that gives me tons of files in shared folders where I am the owner. I tried "'root' in parents" in the query, but that gives me back only files directly under My Drive, while I need also files under subfolders and subolders of those subolders, etc.
I tried also setting the drive parameter but in this case the query does not retrieve anything at all:
driveid = service.files().get(fileId='root').execute()['id']
page_token = None
my_files = list()
while True:
results = service.files().list(q= "'[email protected]' in owners",
pageSize=10,
orderBy='modifiedTime',
pageToken=page_token,
spaces = 'drive',
corpora='drive',
driveId = driveid,
includeItemsFromAllDrives=True,
supportsAllDrives=True,
fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
my_files.extend(items)
page_token = results.get('nextPageToken', None)
if page_token is None:
break
print(len(my_files))
# This prints: 0
How can I get this to work?
I guess the other possibility would be to start from root, get children and recursively navigate the full tree, but that is going to be very slow. The same applies if I get all the files and then find out all the parents to check if they are in My Drive or not, I have too many files and that takes hours.
Thanks in advance!