实现Google Drive API OAuth2.0无需查找验证码的过程 [英] Fulfilling Google Drive API OAuth2.0 Procedure w/o needing to find a verification code

查看:278
本文介绍了实现Google Drive API OAuth2.0无需查找验证码的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Google云端硬盘API在其快速入门中提供了以下OAuth2.0程序以便接收最后的drive_service:

 #从API控制台复制您的凭证
CLIENT_ID ='YOUR_CLIENT_ID'
CLIENT_SECRET ='YOUR_CLIENT_SECRET'

#查看所有可用范围的https://developers.google.com/drive/scopes
OAUTH_SCOPE ='https://www.googleapis.com/ auth / drive'

#为已安装应用程序重定向URI
REDIRECT_URI ='urn:ietf:wg:oauth:2.0:oob'

#文件路径上传
FILENAME ='document.txt'

#运行OAuth流程并检索证书
flow = OAuth2WebServerFlow(CLIENT_ID,CLIENT_SECRET,OAUTH_SCOPE,REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
print'在您的浏览器中转到以下链接:'+ authorize_url
code = raw_input('Enter verify代码:').strip()
credentials = flow.step2_exchange(代码)

#创建一个httplib2.Http对象并使用我们的凭据
http = httplib2对其进行授权。 Http()
http = credentials.authorize(http)

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

请注意,您将获得打印输出的变量authorize_url。您应该使用浏览器访问它,然后确认您允许Google Drive访问您的信息,然后您可以获取验证码。有没有什么办法可以避免手动干预的步骤,并创建一个程序来自动完成这一步? 是的,你可以使用Web服务器来获取OAuth回调,这不需要任何用户交互。基本上,你设置你的服务器来检索oauth代码并添加redirect uri到oauth流所以oauth会将代码发送给指定的uri,而不是告诉用户将代码放入文本框中。



查看方法创建一个名为tools.run_flow()的方法。
它具有非常方便的本地webserver oauth流代码。


The Google Drive API has the following OAuth2.0 procedure from their quickstart to receive the drive_service at the end:

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

Notice that you will be given the variable authorize_url which is printed out. You are supposed to visit it using a browser and then confirm that you allow Google Drive to access your information, which then allows you get a "verification code." Is there any way that I can avoid the step of manual intervention and create a program that automates this step?

解决方案

Yes, you can use web server to get OAuth callback which doesn't require any user interaction.

Basically, you set up your server to retrieve oauth code and add redirect uri to oauth flow so that oauth sends code to given uri instead of telling user to put code into the textbox.

Take a look at tools.run_flow() method at google-api-python-client. It has pretty handy code of local webserver oauth flow.

这篇关于实现Google Drive API OAuth2.0无需查找验证码的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆