代码之家  ›  专栏  ›  技术社区  ›  Pedro Martins de Souza Regeesh Chandran

Python-无法使用Google Drive API从Google Drive下载文件

  •  1
  • Pedro Martins de Souza Regeesh Chandran  · 技术社区  · 6 年前

    起初,我遵循 quickstart sample (效果很好)然后移到 download files sample

    from __future__ import print_function
    from apiclient import discovery
    from googleapiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    import io
    # from oauth2client import client
    # from oauth2client import tools
    
    SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
    
    def main():
    
    
        store = file.Storage('token.json')
        creds = store.get()
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
            creds = tools.run_flow(flow, store)
        service = build('drive', 'v3', http=creds.authorize(Http()))
    
        file_id = 'myfileID'
        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)
    
    
    if __name__ == '__main__':
        main()
    

    但它不起作用,Python无法识别 媒体基础下载

    from __future__ import print_function
    from apiclient import discovery
    from googleapiclient.discovery import build
    from httplib2 import Http
    from oauth2client import file, client, tools
    import io
    # from oauth2client import client
    # from oauth2client import tools
    
    SCOPES = 'https://www.googleapis.com/auth/drive.metadata.readonly'
    
    def main():
    
    
        store = file.Storage('token.json')
        creds = store.get()
        if not creds or creds.invalid:
            flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
            creds = tools.run_flow(flow, store)
        service = build('drive', 'v3', http=creds.authorize(Http()))
    
        file_id = 'myfileID'
        request = service.files().get_media(fileId=file_id)
    
        fh = io.BytesIO()
        with open('mydestinationFile', 'wb') as f:
            f.write(request)
    
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print("Download %d%%." % int(status.progress() * 100))
    
    
    if __name__ == '__main__':
        main()
    

    现在得到错误:

    Traceback (most recent call last):
      File "Google API.py", line 50, in <module>
        main()
      File "Google API.py", line 32, in main
        f.write(request)
    TypeError: a bytes-like object is required, not 'HttpRequest'
    

    我在谷歌上搜索过,甚至在这里搜索过,但没有找到任何类似的东西。我知道我做错了什么,但不知道到底是什么。

    0 回复  |  直到 6 年前
        1
  •  1
  •   Acorn    5 年前

    MediaIoBaseDownload 是中的一种方法 googleapiclient.http

    from googleapiclient.http import MediaIoBaseDownload
    

    我解决了这部分问题,但我仍然可以下载文件。