ZwiTrader
ZwiTrader

Reputation: 345

Google Drive API: Able to download individual files with Python via manually copied file ID, but unable to list file IDs of the given folder

I am working with some folders in a friend's Google Drive account—folders for which which my google service account email has been granted access.

I am able to manually navigate to the desired folder and manually copy a file id(s) from the URL, thereby allowing me to download the file(s) via Python:

def getFile(id):
    request = service.files().get_media(fileId=id)
    fh = io.BytesIO()
    downloader = MediaIoBaseDownload(fh, request)
    done = False
    while done is False:
        status, done = downloader.next_chunk()
        print("Download %d%%." % int(status.progress() * 100))
    fh.seek(0)
    with open(os.path.join('/Users/Desktop/pyTestDownloads', 'file_'+id), 'wb') as f:
        f.write(fh.read())
        f.close()

getFile(manually_copied_gDrive_file_id)

Given that the file(s) properly download to my desktop, I am able to verify that my drive_service account does indeed have access to the folder in question.

However, attempting to retrieve a list of all files in my friend's folder yields an empty list [], whereas retrieving a list of all files from any folder in my personal gDrive (using the same exact function) yields the desired results:

CODE:

fileIDList = []
fileNameList = []

def listFiles(folderID):
    files_list = service.files().list(
        q=" '{0}' in parents and trashed=false ".format(folderID)).execute()
    file_get = files_list.get('files', [])
    print('file_get: ', file_get)

    fileCounter = 0
    for file in file_get:
        fileIDList.append(file['id'])
        fileNameList.append(file['name'])
        print('file ' + str(fileCounter) + ': ' +  file['name'] + ' • ' + file['id'])
        fileCounter += 1

listFiles(friendFolder)
print('friendFolder fileIDList: ', fileIDList)
print('friendFolder fileNameList: ', fileNameList)

print('')

listFiles(myFolder)
print('myFolder fileIDList: ', fileIDList)
print('myFolder fileNameList: ', fileNameList)

OUTPUT:

file_get:  []
friendFolder fileIDList:  []
friendFolder fileNameList:  []

file_get:  [{'kind': 'drive#file', 'id': '1tkQpHoDR7PMn-ymNmLP0MW5J_vJ2Ne1N', 'name': 'Screen Shot 2021-09-23 at 10.33.42 AM.jpg', 'mimeType': 'image/jpeg'}, {'kind': 'drive#file', 'id': '1GqyOf5drrkthC2eL8h2NhsUYQY6Atlcr', 'name': 'Screen Shot 2021-09-23 at 10.17.18 AM.jpg', 'mimeType': 'image/jpeg'}, {'kind': 'drive#file', 'id': '18Z60oxR6rxuzKcro5kowrWhswlUW6a5K', 'name': 'Screen Shot 2021-09-22 at 3.56.57 PM.jpg', 'mimeType': 'image/jpeg'}, {'kind': 'drive#file', 'id': '1RPB38tWJPHTtP9eeZ-UZXm64jR3VAqqU', 'name': 'Screen Shot 2021-09-22 at 10.08.09 AM.jpg', 'mimeType': 'image/jpeg'}]
file 0: Screen Shot 2021-09-23 at 10.33.42 AM.jpg • 1tkQpHoDR7PMn-ymNmLP0MW5J_vJ2Ne1N
file 1: Screen Shot 2021-09-23 at 10.17.18 AM.jpg • 1GqyOf5drrkthC2eL8h2NhsUYQY6Atlcr
file 2: Screen Shot 2021-09-22 at 3.56.57 PM.jpg • 18Z60oxR6rxuzKcro5kowrWhswlUW6a5K
file 3: Screen Shot 2021-09-22 at 10.08.09 AM.jpg • 1RPB38tWJPHTtP9eeZ-UZXm64jR3VAqqU
myFolder fileIDList:  ['1tkQpHoDR7PMn-ymNmLP0MW5J_vJ2Ne1N', '1GqyOf5drrkthC2eL8h2NhsUYQY6Atlcr', '18Z60oxR6rxuzKcro5kowrWhswlUW6a5K', '1RPB38tWJPHTtP9eeZ-UZXm64jR3VAqqU']
myFolder fileNameList:  ['Screen Shot 2021-09-23 at 10.33.42 AM.jpg', 'Screen Shot 2021-09-23 at 10.17.18 AM.jpg', 'Screen Shot 2021-09-22 at 3.56.57 PM.jpg', 'Screen Shot 2021-09-22 at 10.08.09 AM.jpg']

Any ideas as to why I am encountering this problem? Any possible solutions?

Upvotes: 2

Views: 243

Answers (1)

Jacques-Guzel Heron
Jacques-Guzel Heron

Reputation: 2598

To accomplish your goal of listing every file into that folder you need to include two more parameters into the Files.list method: includeItemsFromAllDrives and supportsAllDrives; both on True.

Upvotes: 2

Related Questions