gpt4 book ai didi

python - Pydrive google drive 自动认证

转载 作者:太空狗 更新时间:2023-10-29 21:04:49 24 4
gpt4 key购买 nike

我有以下代码:

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.

问题是 Google Chrome 中会生成验证码,每次用户都需要将其复制-> 粘贴到控制台以进行身份​​验证。有没有办法使这个过程自动化?

最佳答案

实际上你需要通过以下代码将client_secret.json文件复制到my_cred.txt:

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

然后使用下面的代码初始化驱动:

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

查看此链接的更多内容: Automating pydrive verification process

关于python - Pydrive google drive 自动认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46978784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com