【问题标题】:i want to download file from google drive using Drive Api我想使用 Drive Api 从谷歌驱动器下载文件
【发布时间】:2018-04-12 07:13:29
【问题描述】:

在这段代码中:

file_id = '0BwwA4oUTeiV1UVNwOHItT0xfa2M'
request = drive_service.files().get_media(fileId=file_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)

我不知道如何获取 file_id ,我在上传时获取了 file_id 但现在我无法弄清楚如何获取 Google Drive 上存在的文件的 file_id。

例如。如果我上传的文件名为 A001002.pdf ,我如何获取该文件的文件 ID。 网上有一些参考资料看不懂。

链接:files.list

有什么帮助吗?

【问题讨论】:

    标签: python google-api google-drive-api google-api-python-client google-apis-explorer


    【解决方案1】:

    file.list 方法包含一个用于搜索的 q 参数

    GET https://www.googleapis.com/drive/v3/files?q=name+%3D+'hello'&key={YOUR_API_KEY}

    Python 猜测

    """
    Shows basic usage of the Drive v3 API.
    
    Creates a Drive v3 API service and prints the names and ids of the last 10 files
    the user has access to.
    """
    from __future__ import print_function
    from apiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    
    # Setup the Drive v3 API
    SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
    store = file.Storage('credentials.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('drive', 'v3', http=creds.authorize(Http()))
    
    # Call the Drive v3 API
    results = service.files().list(
        pageSize=10, fields="*").execute()
    items = results.get('files', [])
    if not items:
        print('No files found.')
    else:
        print('Files:')
        for item in items:
            print('{0} ({1})'.format(item['name'], item['id']))
    

    请注意,此示例未显示如何添加附加参数,我仍在谷歌搜索,但我不是 python 开发人员,您可能比我更了解如何执行此操作。

    【讨论】:

    • 我能够获取文件 ID,但这些文件在驱动器上不可见,我的上传文件是否会转到隐藏的文件夹等。{u'id': u'1DXB8jEKibjK_R5wdxCOLdHH7wVrIU1Vq', u' name': u'abcd'} } 像这样,但我上传的名为“abcd”的文件不可见
    • 您是否使用服务帐户进行身份验证。如果是这样,您可能正在其帐户上上传。如果不是,您可能需要在登录错误帐户时重新验证您的代码。
    • 尝试拨打developers.google.com/drive/v3/reference/about/get 电话,它会告诉您您的登录身份。
    【解决方案2】:

    根据您的实施,还有另一种选择。如果您不需要以编程方式获取 fileID,您只需从浏览器在 google docs 中打开文件,ID 就会显示在 URL 中。

    【讨论】:

    • 请让您的回答更加充实;现有的答案可以作为灵感。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多