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

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

09-06 10:35