Google云端硬盘API从快速入门开始就有以下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云端硬盘访问您的信息,然后允许您获取“验证码”。有什么方法可以避免手动干预的步骤,而可以创建一个自动执行此步骤的程序?
是的,您可以使用Web服务器获取OAuth回调,而无需任何用户交互。
基本上,您将服务器设置为检索oauth代码,并将重定向uri添加到oauth流中,以便oauth将代码发送到给定的uri,而不是告诉用户将代码放入文本框。
看看google-api-python-client上的tools.run_flow()方法。它具有本地Web服务器oauth流程的便捷代码。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句