Pydrive google drive automate authentication(Pydrive 谷歌驱动器自动身份验证)
本文介绍了Pydrive 谷歌驱动器自动身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
from pydrive.auth import GoogleAuth
gauth = GoogleAuth()
gauth.DEFAULT_SETTINGS = {'save_credentials': True,'client_config_backend': 'settings',
'oauth_scope': ['https://www.googleapis.com/auth/drive'],
'get_refresh_token': True,
'save_credentials_file':"credential_log.txt",
'save_credentials_backend': 'file'}
gauth.client_config = {'client_id': '499039293801-krogpnentl6qk035vt4hcd36nefiautt.apps.googleusercontent.com', 'client_secret': 'iqFCuOh36amMFi3U1dkyCWJK',
'redirect_uri':'urn:ietf:wg:oauth:2.0:oob','revoke_uri': 'None',
'token_uri':'https://accounts.google.com/o/oauth2/token',
'auth_uri':'https://accounts.google.com/o/oauth2/auth',
'save_credentials_file':"mycreds_p2iman.txt"}
gauth.CommandLineAuth()
from pydrive.drive import GoogleDrive
drive = GoogleDrive(gauth)
file4 = drive.CreateFile({'title':'somethingdifferent.txt', 'mimeType':'different/txt'})
file4.SetContentString('My name is John')
file4.Upload() # Upload file.
file4.SetContentString('My name is John')
file4.Upload() # Update content of the file.
问题是在谷歌浏览器中生成了一个验证码,每次用户需要复制->将其粘贴到控制台中以进行身份验证.有没有办法自动化这个过程?
The problem is that a verification code is generated in the Google Chrome and every time user needs to copy->paste it in the console in order to authenticate. Is there a way to automate this process?
推荐答案
其实需要将client_secret.json文件复制到my_cred.txt中,代码如下:
Actually you need to copy the client_secret.json file to my_cred.txt by the following code:
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")
然后使用以下代码初始化驱动:
Then use the following code to initialize the drive:
def authorize_drive():
gauth = GoogleAuth()
gauth.DEFAULT_SETTINGS['client_config_file'] = "client_secret.json"
gauth.LoadCredentialsFile("mycreds.txt")
return GoogleDrive(gauth)
class DriveReport(object):
def __init__(self):
self.drive = authorize_drive()
查看更多此链接:自动化pydrive验证过程
这篇关于Pydrive 谷歌驱动器自动身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Pydrive 谷歌驱动器自动身份验证
基础教程推荐
猜你喜欢
- 使用Python匹配Stata加权xtil命令的确定方法? 2022-01-01
- 如何在 Python 中检测文件是否为二进制(非文本)文 2022-01-01
- 如何在Python中绘制多元函数? 2022-01-01
- 症状类型错误:无法确定关系的真值 2022-01-01
- 合并具有多索引的两个数据帧 2022-01-01
- 使 Python 脚本在 Windows 上运行而不指定“.py";延期 2022-01-01
- 使用 Google App Engine (Python) 将文件上传到 Google Cloud Storage 2022-01-01
- 哪些 Python 包提供独立的事件系统? 2022-01-01
- Python 的 List 是如何实现的? 2022-01-01
- 将 YAML 文件转换为 python dict 2022-01-01