无法使用python列出Google驱动器中的文件

4ioopgfo  于 2022-12-05  发布在  Python
关注(0)|答案(1)|浏览(133)

不确定这是否与我的代码或谷歌方面的东西有关,但我可以将文件推送到驱动器,但由于某些原因,我不能列出文件夹内的文件/文件夹元数据。

SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = 'creds.json'
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=credentials)

topFolderId = '0AAYXadsMHp8IUk9PVA'

items = []
pageToken = ""
while pageToken is not None:
    response = service.files().list(q="'" + topFolderId + "' in parents", pageSize=1000, pageToken=pageToken, fields="nextPageToken, files(id, name)").execute()
    items.extend(response.get('files', []))
    pageToken = response.get('nextPageToken')

任何想法在这里,我不认为这是权限相关,因为我可以把文件在驱动器,只是没有列出它们。

o8x7eapl

o8x7eapl1#

我想你忘了。execute()

try:
    service = build('drive', 'v3', credentials=creds)

    # Call the Drive v3 API
    results = service.files().list(q="'" + topFolderId + "' in parents",
        pageSize=10, fields="nextPageToken, files(id, name)").execute()
    items = results.get('files', [])

    if not items:
        print('No files found.')
        return
    print('Files:')
    for item in items:
        print(u'{0} ({1})'.format(item['name'], item['id']))
except HttpError as error:
    # TODO(developer) - Handle errors from drive API.
    print(f'An error occurred: {error}')

相关问题