'Google Drive API to list folders in Python

I have a bunch of folders in my personal Google Drive, each starting with "Renamed". I want to list these. I used the snippet in the official documentation but it doesn't print anything:

from googleapiclient.discovery import build
from google.oauth2 import service_account 

credentials = service_account.Credentials.from_service_account_file(
            service_account_path,
            scopes=['https://www.googleapis.com/auth/drive']
)
service = build(api_name='drive', api_version='v3', credentials=credentials)

page_token = None
while True:
    response = service.files().list(q="(name contains \"Renamed\") and (mimeType=\"application/vnd.google-apps.folder\")",
                                        spaces='drive',
                                        fields='nextPageToken, files(id, name)',
                                        pageToken=page_token).execute()
    for file in response.get('files', []):
        print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break  

I then included some additional parameters in the call; again no folders are found:

page_token = None
while True:
    response = service.files().list(q="(name contains \"Renamed\") and (mimeType=\"application/vnd.google-apps.folder\")",
                                        spaces='drive',
                                        fields='nextPageToken, files(id, name)',
                                        corpora='allDrives',
                                        includeItemsFromAllDrives='true',
                                        supportsAllDrives='true',
                                        pageToken=page_token).execute()
    for file in response.get('files', []):
        print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break  

When dropping the mimeType part from the q parameter and keeping only: q="name contains \"Renamed\"", it still didn't print any of the folders. But when I modified it to q="name contains \"Central\"", it successfully found a spreadsheet called "Central Sheet". It seems I am doing something wrong when listing folders but I can successfully retrieve files. What am I missing?

Note: I tried running the query also here and it returned the folders in question correctly. I then updated the fields and q parameters accordingly to: q="name contains \"Renamed\"" and fields="files,incompleteSearch,kind,nextPageToken". Still no folders are retrieved.



Solution 1:[1]

It is quite odd. I have copy/paste the code snippets shared and after having created several folders in my account with names that include 'Renamed' I am able to successfully get the entire folder list.

As seen below first snippet and result below:

enter image description here

And below the second snippet with its result:

enter image description here

Should you need to take a look at my code see it below:

from googleapiclient.discovery import build
from google.oauth2 import service_account 

SERVICE_ACCOUNT_FILE = 'API KEY' #Your API KEY goes here
SCOPES = ['https://www.googleapis.com/auth/drive']

credentials = service_account.Credentials.from_service_account_file(
            SERVICE_ACCOUNT_FILE, scopes=SCOPES)
delegated_creds = credentials.with_subject('[email protected]')

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

""" page_token = None
while True:
    response = service.files().list(q="(name contains \"Renamed\") and (mimeType=\"application/vnd.google-apps.folder\")",
                                        spaces='drive',
                                        fields='nextPageToken, files(id, name)',
                                        pageToken=page_token).execute()
    for file in response.get('files', []):
        print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break  """

page_token = None
while True:
    response = service.files().list(q="(name contains \"Renamed\") and (mimeType=\"application/vnd.google-apps.folder\")",
                                        spaces='drive',
                                        fields='nextPageToken, files(id, name)',
                                        corpora='allDrives',
                                        includeItemsFromAllDrives='true',
                                        supportsAllDrives='true',
                                        pageToken=page_token).execute()
    for file in response.get('files', []):
        print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
    page_token = response.get('nextPageToken', None)
    if page_token is None:
        break   

You can remove comments as needed. So this might be something local in your environment. Something I noticed is that you are using a service account but no impersonation has been declared, you may want to double check that on your end.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1