Not getting list of items in shared folders with Google Drive API

Viewed 703

I have tried to get the list of folders with Google Drive API in Python. I can get the list in my drive folders but I can not get the list of items of shared folders. I use GCP service account for credential and I already set the service account into shared folder. Anyone has solutions?? Should I use another parameter?? Thank you so much.

drive = build('drive', 'v3', credentials=credentials)

folderId = "xxxxxxxxxxx"

query = f"parents = '{folderId}'"
response = drive.files().list(q=query,includeItemsFromAllDrives=True,supportsAllDrives=True).execute()

files = response.get('files')
nextPageToken = response.get('nextPageToken')

while nextPageToken:
    response = drive.files().list(q=query,includeItemsFromAllDrives=True,supportsAllDrives=True).execute()
    files.extend(response.get('files'))
    nextPageToken = response.get('nextPageToken')

df = pd.DataFrame(files)
1 Answers

Your issue is the way you are calling parents

query = f"parents = '{folderId}'"

Parents = should be parents in

query = f"parents in '{folderId}'"

background info in the event you have the wrong file id for the folder.

Start simple when you are working with the Q parameter. Can you find the folder by searching for the mimetype?

page_token = None
while True:
    response = drive_service.files().list(q="mimeType='application/vnd.google-apps.folder'",
                                          
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):
        # Process change
        print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break

Parents in

Assuming that you get the folder back in the previous call you need to take the file id of that call and send it to this call.

page_token = None
while True:
    response = drive_service.files().list(q="parents in 'FILEIDFROMPREVIOUSCALL'",
                                          
                                          fields='nextPageToken, files(id, name)',
                                          pageToken=page_token).execute()
    for file in response.get('files', []):
        # Process change
        print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break
Related