gpt4 book ai didi

python - 无需查找验证码即可完成Google Drive API OAuth 2.0 Procedure

转载 作者:太空宇宙 更新时间:2023-11-03 11:31:45 25 4
gpt4 key购买 nike

Google Drive API 具有以下来自其 quickstart 的 OAuth2.0 过程最后接收 drive_service:

# Copy your credentials from the APIs Console
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'

# Check https://developers.google.com/drive/scopes for all available scopes
OAUTH_SCOPE = 'https://www.googleapis.com/auth/drive'

# Redirect URI for installed apps
REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'

# Path to the file to upload
FILENAME = 'document.txt'

# Run through the OAuth flow and retrieve credentials
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)

# Create an httplib2.Http object and authorize it with our credentials
http = httplib2.Http()
http = credentials.authorize(http)

drive_service = build('drive', 'v2', http=http)

请注意,您将获得打印出来的变量 authorize_url。您应该使用浏览器访问它,然后确认您允许 Google Drive 访问您的信息,然后您将获得一个“验证码”。有什么方法可以避免手动干预步骤并创建一个自动执行此步骤的程序?

最佳答案

是的,您可以使用网络服务器获取不需要任何用户交互的 OAuth 回调。

基本上,您将服务器设置为检索 oauth 代码并将重定向 uri 添加到 oauth 流程,以便 oauth 将代码发送到给定的 uri,而不是告诉用户将代码放入文本框。

看看tools.run_flow() google-api-python-client 中的方法。它有非常方便的本地网络服务器 oauth 流程代码。

关于python - 无需查找验证码即可完成Google Drive API OAuth 2.0 Procedure,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17892504/

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