我正在使用 Google Colaboratory notebook 开发一个 Python (3.6) 项目,我需要访问 Google Drive 上的文件。使用下面的代码,我可以读取 Drive 上的文件,但每次运行代码块时它都会要求输入安全代码,因此每当 session 超时时,我都需要再次获取安全代码。
drive.mount('/content/drive')
有没有更好更方便的方法呢?持续的身份验证非常烦人。
最佳答案
您可以尝试使用 PyDrive。 PyDrive是 google-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/