python - Colaboratory - 记住 Google Drive Auth(Python)

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

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

drive.mount('/content/drive')

有没有更好更方便的方法呢?持续的身份验证非常烦人。

最佳答案

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

下面的片段是来自 this example 的片段.它演示了代码的身份验证部分。

!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 .

关于python - Colaboratory - 记住 Google Drive Auth(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53695779/

相关文章:

python - 无法修复 "zipimport.ZipImportError: can' t 解压数据; zlib 不可用”当我输入 "python3.6 get-pip.py"

python - 设置两个 matplotlib imshow 图具有相同的颜色图比例

google-drive-api - Google Drive SDK 更新共享权限

javascript - Google Drive API 根据共享云端硬盘中的所有者列出文件

google-apps-script - 如何通过应用程序脚本在特定文件夹中创建电子表格

python - 如何强制 pytest 运行每个测试(停止跳过未标记为跳过的测试)?

python - pySerial readline 读取之前写入的数据

c# - 找不到命名空间 System.IdentityModel.Services

php - 在某些条件下动态更改 WP_Error 消息 CSS?

Java & MySQL - 创建登录认证