我正在使用Pydrive模块上传文件到我的谷歌驱动器。我正在使用的代码:
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file1 = drive.CreateFile({'title': 'hello.txt'})
file1.SetContentString('Hello world project')
file1.Upload()当我运行这段代码时,浏览器会打开,我需要点击google帐户进行登录。此过程每次运行代码时都会重复。有没有一种方法,在运行代码时,文件自动上传?比如在代码中提供谷歌电子邮件帐户和密码。
发布于 2022-04-07 05:28:17
最初在这个Question中回答
若要通过PyDrive模块将文件自动上载到Google,请使用此代码模板
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
# ---Try to load saved client credentials ---#
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
# ------------------------------------------ #
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file1 = drive.CreateFile()
file1.SetContentFile('shoe.png')
file1.Upload()https://stackoverflow.com/questions/71776367
复制相似问题