dat*_*den 6 python cookies flask jwt
我有以下端点开始授权流程:
@spotify_auth_bp.route("/index", methods=['GET', 'POST'])
def spotify_index():
CODE = "code"
CLIENT_ID = os.environ.get('SPOTIPY_CLIENT_ID')
SCOPE = os.environ.get('SPOTIPY_SCOPE')
REDIRECT_URI = os.environ.get('SPOTIPY_REDIRECT_URI')
SPOTIFY_AUTH_URL = "https://accounts.spotify.com/authorize"
return redirect("{}?response_type={}&client_id={}&scope={}&redirect_uri={}".format(
SPOTIFY_AUTH_URL, CODE, CLIENT_ID, SCOPE, REDIRECT_URI), code=302)
Run Code Online (Sandbox Code Playgroud)
然后我从 Spotify 重定向回/callback
,我jwt
在响应中设置cookie,如下所示:
@spotify_auth_bp.route("/callback", methods=['GET', 'POST'])
def spotify_callback():
token = user.encode_access_token(access_token)
a11n_h, a11n_d, a11n_s = token.decode().split('.')
response = make_response(redirect('http://localhost/about', code=302))
response.set_cookie('a11n.h', a11n_h)
response.set_cookie('a11n.d', a11n_d)
response.set_cookie('a11n.s', a11n_s, httponly=True)
return response
Run Code Online (Sandbox Code Playgroud)
cookie 显示在我的浏览器控制台中的“应用程序”下。
现在我想从另一个端点获取它们,如下所示:
@spotify_auth_bp.route("/get_token/<user_id>", methods=['GET', 'POST'])
def get_token(user_id):
# get access token cookies
a11n_h = request.cookies.get('a11n.h')
a11n_d = request.cookies.get('a11n.d')
a11n_s = request.cookies.get('a11n.s')
Run Code Online (Sandbox Code Playgroud)
但是我将这些 cookie 打印为None
, None
,None
另外,我没有Flask
配置...
app.config.update(
SESSION_COOKIE_SECURE=True,
SESSION_COOKIE_SAMESITE='Lax',
)
Run Code Online (Sandbox Code Playgroud)
...这可能会阻止发送 cookie http
。
我错过了什么?
OBS:我正在使用 测试此端点Postman
,并且在 Headers 中,我已将键Access-Control-Allow-Credentials
设置为 value true
。
小智 4
根据上述内容,我假设您正在使用基于任何其他框架的前端应用程序,并使用 axios、fetch、request 等库来访问 Flask 上的 API。
因此,您可能错过了需要在请求中设置一个标志以允许发送 cookie。请参阅以下链接以查找实现方法:
fetch('https://example.com', {
credentials: 'include'
});
Run Code Online (Sandbox Code Playgroud)
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://example.com/', true);
xhr.withCredentials = true;
xhr.send(null);
Run Code Online (Sandbox Code Playgroud)
如果不能解决问题,请纠正我。
归档时间: |
|
查看次数: |
535 次 |
最近记录: |