简体   繁体   English

重定向到带有标题的OAuth URL

[英]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.

相关问题 django-allauth重定向URL使用Stripe OAuth提示“登录失败” - django-allauth redirect url says “Login Failure” with Stripe OAuth Redirect URL issue with Gmail Api and using OAuth in Python - Redirect URL issue with Gmail Api and using OAuth in Python 检索作为POST响应提供的重定向URL中的OAuth代码 - Retrieve OAuth code in redirect URL provided as POST response OAuth登录完成后跳转到上一个URL(flask-dance) - Redirect to previous URL after OAuth login is completed (flask-dance) 如何在Django测试案例中使用正确的oAuth2重定向网址? - How to use correct oAuth2 redirect url in Django test case? 如何在松弛 oAuth 重定向 url 中使用动态重定向网址? - How can I use dynamic redirect urls in slack oAuth redirect url? 尝试为 Zoom OAuth API 请求访问令牌会导致重定向 URL 无效 - Attempt to request Access token for Zoom OAuth API results in invalid redirect url 重新输入重定向 URL 时获取“spotipy.oauth2.SpotifyOauthError: Bad Request” - Getting 'spotipy.oauth2.SpotifyOauthError: Bad Request' when re-inputting the redirect URL Tweepy OAuth 2.0 授权代码流程与 PKCE 本机应用程序 - 我将什么值用于回调 URI/重定向 URL 和网站 ZE6B391A8D2C4D45902A23A8B658? - Tweepy OAuth 2.0 Authorization Code Flow with PKCE Native App - what values do I use for Callback URI/Redirect URL & Website URL? 没有要重定向到的 URL - No URL to redirect to
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM