首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Pydrive实现自动上传

使用Pydrive实现自动上传
EN

Stack Overflow用户
提问于 2022-04-07 04:31:20
回答 1查看 342关注 0票数 0

我正在使用Pydrive模块上传文件到我的谷歌驱动器。我正在使用的代码:

代码语言:javascript
复制
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帐户进行登录。此过程每次运行代码时都会重复。有没有一种方法,在运行代码时,文件自动上传?比如在代码中提供谷歌电子邮件帐户和密码。

EN

回答 1

Stack Overflow用户

发布于 2022-04-07 05:28:17

最初在这个Question中回答

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

代码语言:javascript
复制
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()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71776367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档