列出Google驱动器文件夹中的文件和文件夹

时间:2018-11-01 19:24:01

标签: python-3.x google-api-client

我正在尝试获取特定Google驱动器文件夹中文件和文件夹的列表。

from oauth2client.service_account import ServiceAccountCredentials
from googleapiclient.discovery import build

scope = [
        'https://spreadsheets.google.com/feeds',
        'https://www.googleapis.com/auth/drive'
    ]
credentials = ServiceAccountCredentials.from_json_keyfile_name('creds.json', scope)

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

我正在使用此功能,但肯定缺少某些内容。请指导!

def retrieve_all_files(service):

    result = []
    page_token = None
    while True:
        try:
            param = {}
            if page_token:
                param['pageToken'] = page_token
            files = service.files().list(**param).execute()

            result.extend(files['items'])
            page_token = files.get('nextPageToken')
            if not page_token:
                break   
        except:
            print('An error occurred')
            break
    return result

0 个答案:

没有答案