[英]Redirect to OAuth URL with headers
I'm trying to write a Python 3.5 Flask application that redirects a user to an OAuth URL, for authentication / authorization. 我正在尝试编写一个Python 3.5 Flask应用程序,该应用程序将用户重定向到OAuth URL,以进行身份验证/授权。 As part of that redirection, I have to include the
Authorization
header. 作为重定向的一部分,我必须包括
Authorization
标头。 The built-in redirect()
method in Flask doesn't seem to support adding HTTP headers. Flask中的内置
redirect()
方法似乎不支持添加HTTP标头。
What's the proper way of handling this in such an application? 在这样的应用程序中处理此问题的正确方法是什么?
You will need to build your own response object to add headers. 您将需要构建自己的响应对象以添加标题。 You can check out the docs here: http://docs.python-requests.org/en/master/api/#requests.Response
您可以在此处查看文档: http : //docs.python-requests.org/en/master/api/#requests.Response
A simple example for your use case would be something like: 您的用例的一个简单示例如下:
response = Response(headers={'Authorization': 'whatever'},
is_redirect=True,
url="https://your-redirected-url.com")
return response
Edit: Further info 编辑:更多信息
Also, I would check out https://github.com/lepture/flask-oauthlib if you are interested in using a library. 另外,如果您对使用库感兴趣,我会查看https://github.com/lepture/flask-oauthlib 。 It has support for oAuth1 and oAuth2 and it is relatively easy to setup with a standard Flask app.
它支持oAuth1和oAuth2,并且相对容易使用标准Flask应用进行设置。
Edit: Another way of doing it 编辑:另一种方式
This morning I remembered a simpler way to do this. 今天早上,我想起了一种更简单的方法。 You can call the redirect function and it will return a flask Response object.
您可以调用重定向函数,它将返回烧瓶响应对象。 Then you are able to set the headers on that newly created object.
然后,您可以在该新创建的对象上设置标题。
response = redirect('https://url')
response.headers = {'authorization': 'whatever'}
return response
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.