I need help with a Python script that uses the Google Drive API. I want to find files in my Drive that I didn’t create, but only those in shared folders where others can add stuff. I don’t want to see files shared with me from other accounts.
I’m using this code to get files not owned by me:
result = drive_service.files().list(
pageSize=50,
fields='nextPageToken, files(*)',
q='not "[email protected]" in owners'
).execute()
This works but also shows files shared from other accounts. I can’t figure out how to tell these apart. The API docs don’t seem to have a clear way to do this.
I’ve looked at other solutions, but they only search the main folder. I need to check everything in my Drive.
Is there a way to do this? Any tips would be great. Thanks!
Having worked extensively with the Drive API, I can offer some insights. Your approach is on the right track, but you need to refine your query. Try this:
result = drive_service.files().list(
pageSize=50,
fields=‘nextPageToken, files(id, name, owners, sharedWithMe)’,
q=“not ‘me’ in owners and sharedWithMe = false and ‘me’ in writers”,
supportsAllDrives=True,
includeItemsFromAllDrives=True
).execute()
This query filters for files not owned by you, not directly shared, but where you have write access. It should capture files in shared folders where others can add content. The ‘supportsAllDrives’ and ‘includeItemsFromAllDrives’ parameters ensure you’re searching across all accessible drives.
Remember to handle pagination for large result sets. You might also want to add ‘and trashed = false’ to exclude deleted items. Let me know if you need any clarification on implementing this solution.
have you tried adding ‘sharedWithMe = false’ to your query? that might help filter out files shared from other accounts. also, consider using ‘not me in owners’ instead of your specific email. if you’re dealing with shared drives, you’ll need to add ‘supportsAllDrives=True’ and ‘includeItemsFromAllDrives=True’ to your request. hope this helps!
I’ve dealt with similar issues using the Drive API. One approach that worked for me was combining multiple query parameters. Try something like this:
q=‘not me in owners and sharedWithMe = false and trashed = false’
This filters out files you own, those shared directly with you, and any trash items. You might also want to add ‘and mimeType != ‘application/vnd.google-apps.folder’’ to exclude folders from the results.
For shared drives, make sure to include ‘supportsAllDrives=True’ and ‘includeItemsFromAllDrives=True’ in your request parameters. This ensures you’re searching across all accessible drives.
Remember to handle pagination if you have a lot of files. The ‘nextPageToken’ in the response can help with that.
Hope this helps point you in the right direction. Let me know if you need any clarification on implementing these suggestions.