使用Python&获取Google云端硬盘中文件夹的内容APICLient

时间:2016-02-24 18:27:02

标签: python google-drive-api

我试图使用Python中的apiclient模块获取Google云端硬盘文件夹的内容。

这是我的代码

    #!/usr/bin/env python

    from __future__ import print_function
    import os

    from apiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    import requests
    try:
        import argparse
        flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
    except ImportError:
        flags = None

    SCOPES = 'https://www.googleapis.com/auth/drive.file'
    store = file.Storage('storage.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
        creds = tools.run_flow(flow, store, flags) \
                if flags else tools.run(flow, store)
    DRIVE = build('drive', 'v2', http=creds.authorize(Http()))
    print (DRIVE.children().list( folderId='# id of the folder').execute()['items'])

然而,我得到的只是一个空列表而不是填充列表。我知道文件夹里面有文件。

我在这里引用代码。 Google Drive folder ID

建议

1 个答案:

答案 0 :(得分:0)

我猜你没有在creds = tools.run_flow(flow, store, flags)中添加if-statement

if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)
        creds = tools.run_flow(flow, store, flags) \
                if flags else tools.run(flow, store)

这应该是:

if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secrets.json', SCOPES)

        if flags:
            creds = tools.run_flow(flow, store, flags)
        else:
            creds = tools.run(flow, store)

您可以在此Google documentation中找到它。