I’m working on a Python script to search my Google Drive using the API. I want to find files that I don’t own but are in folders I share with others, while skipping files that others have shared with me. I tried the following snippet:
result = service.files().list(
pageSize=50,
fields='nextPageToken, files(*)',
q="not '[email protected]' in owners",
pageToken=token
).execute()
Although it finds non-owned files, it also returns files shared from other drives. I’ve checked the API documentation and experimented with the includeItemsFromAllDrives and supportsAllDrives parameters, but they don’t resolve this issue. Is there any way to adjust the query so it omits these shared files while still searching my entire drive?
hey jess, hav u tried using the ‘corpus’ parameter? Set it to ‘user’ to limit search to ur personal drive. like this:
q=“not ‘[email protected]’ in owners and ‘me’ in parents”
corpus=‘user’
this might solve ur problem without needing complex queries. lemme know if it works!
Have you considered using the ‘spaces’ parameter in your API call? Setting it to ‘drive’ might help filter out files from shared drives. Something like this could work:
service.files().list(
pageSize=50,
fields=‘nextPageToken, files(*)’,
q=“not ‘[email protected]’ in owners and ‘me’ in parents”,
spaces=‘drive’,
pageToken=token
).execute()
This should limit results to your personal drive while still including files in shared folders you have access to. The ‘me’ in parents part ensures you’re only getting files in folders you can see directly.
If that doesn’t solve it completely, you might need to implement some post-processing on the results to further filter out unwanted items based on their metadata.
I’ve faced a similar challenge when working with the Google Drive API. One approach that worked for me was to modify the query to specifically target files within your drive. You can do this by adding a condition to check for the ‘me’ in parents parameter. Here’s an adjusted query that might help:
q=“‘me’ in owners or (‘me’ in parents and not ‘me’ in owners)”
This query will return files you own, as well as files in folders you own but aren’t the owner of. It should exclude files shared from other drives.
Another trick I found useful was to use the ‘driveId’ parameter along with the ‘corpora’ set to ‘drive’ to limit the search to your specific drive. You’ll need to get your driveId first, but once you have it, you can add these parameters to your list() method call.
If you’re still having trouble, you might want to consider using the drive.files.list endpoint instead of files.list, as it gives you more granular control over which files are returned.