【问题标题】:Colaboratory - Remember Google Drive Auth (Python)Colaboratory - 记住 Google Drive Auth (Python)
【发布时间】:2019-05-10 18:28:01
【问题描述】:

我正在使用 Google Colaboratory notebook 进行 Python (3.6) 项目,我需要访问 Google Drive 上的文件。使用下面的代码,我可以读取 Drive 上的文件,但每次运行代码块时都会要求输入安全代码,因此每当会话超时时,我都需要再次获取安全代码。

drive.mount('/content/drive')

有没有更好更方便的方法来做到这一点?持续的身份验证很烦人。

【问题讨论】:

    标签: python authentication google-drive-api google-colaboratory


    【解决方案1】:

    您可以尝试使用 PyDrive。 PyDrivegoogle-api-python-client 的包装库,可简化许多常见的 Google Drive API 任务。

    下面的 sn-p 是来自 this example 的 sn-p。它演示了代码的身份验证部分。

    !pip install -U -q PyDrive
    
    from pydrive.auth import GoogleAuth
    from pydrive.drive import GoogleDrive
    from google.colab import auth
    from oauth2client.client import GoogleCredentials
    
    # 1. Authenticate and create the PyDrive client.
    auth.authenticate_user()
    gauth = GoogleAuth()
    gauth.credentials = GoogleCredentials.get_application_default()
    drive = GoogleDrive(gauth)
    

    如果您需要更多示例,可以访问PyDrive documentation

    【讨论】:

    • 干杯谢谢!我应该花点时间更好地了解 PyDrive。
    猜你喜欢
    • 2019-04-20
    • 2018-07-29
    • 2018-10-14
    • 2012-03-22
    • 2018-07-13
    • 2020-12-23
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多