【问题标题】:Using Pydrive for automatic uploading使用 Pydrive 进行自动上传
【发布时间】:2022-07-31 02:48:30
【问题描述】:

我正在使用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 帐户进行登录。每次运行代码时都会重复此过程。有没有办法在运行代码时自动上传文件?比如在代码中提供谷歌邮箱账号和密码。

【问题讨论】:

  • 查看文档,似乎有 LoadCredentials 和 SaveCredentials 函数

标签: python google-oauth pydrive


【解决方案1】:

最初在这个Question回答

要通过 PyDrive 模块自动将文件上传到 Google Drive,请使用此代码模板

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()

【讨论】:

    【解决方案2】:

    您好,这可以工作一小时,然后 refresh() 失败,有没有办法解决这个问题?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多