No Google Drive files are listed in Drive API request

无人久伴 提交于 2019-12-05 09:34:53
Alain

Please correct me if I'm wrong but I believe you are using the https://www.googleapis.com/auth/drive.file scope, which only returns files that your app has created or have been explicitly opened with your app using the Google Drive UI or the Picker API.

To retrieve all files, you will need to use the broader scope: https://www.googleapis.com/auth/drive.

To learn more about the different scopes, have a look at the documentation.

For one thing, you need to iterate through page_token in order to get all the contents of My Drive as well as any subfolders. There are a few other things it could be too like not providing a query, etc. Try this:

def retrieve_all_files(service):
    """ RETURNS a list of files, where each file is a dictionary containing
        keys: [name, id, parents]
    """

    query = "trashed=false"

    page_token = None
    L = []

    while True:
        response = service.files().list(q=query,
                                             spaces='drive',
                                             fields='nextPageToken, files(id, name, parents)',
                                             pageToken=page_token).execute()
        for file in response.get('files', []):  # The second argument is the default
            L.append({"name":file.get('name'), "id":file.get('id'), "parents":file.get('parents')})

        page_token = response.get('nextPageToken', None)  # The second argument is the default

        if page_token is None:  # The base My Drive folder has None
            break

    return L
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!