【问题标题】:How to POST the refresh token to Flask JWT Extended?如何将刷新令牌发布到 Flask JWT Extended?
【发布时间】:2019-08-01 01:27:37
【问题描述】:

我正在尝试从此处的代码中刷新 JWT 令牌。问题在于如何通过刷新获取新令牌。

这行得通:

curl http://127.0.0.1:5000/protected
{"msg":"Missing Authorization Header"}

这行得通,我得到我的令牌并将其放入 ACCESS

curl -H "Content-Type: application/json" -X POST   -d '{"username":"test","password":"test"}' http://localhost:5000/login

这行得通,我得到了我的用户名

curl -H "Authorization: Bearer $ACCESS" http://localhost:5000/protected

但是当令牌过期时,如何使用我的刷新令牌和/或访问令牌获取 curl 以获取我的新访问令牌?我尝试了许多 POST,但似乎没有任何效果:

https://flask-jwt-extended.readthedocs.io/en/latest/refresh_tokens.html

from flask import Flask, jsonify, request
from flask_jwt_extended import (
    JWTManager, jwt_required, create_access_token,
    jwt_refresh_token_required, create_refresh_token,
    get_jwt_identity
)

app = Flask(__name__)

app.config['JWT_SECRET_KEY'] = 'super-secret'  # Change this!
jwt = JWTManager(app)


@app.route('/login', methods=['POST'])
def login():
    username = request.json.get('username', None)
    password = request.json.get('password', None)
    if username != 'test' or password != 'test':
        return jsonify({"msg": "Bad username or password"}), 401

    # Use create_access_token() and create_refresh_token() to create our
    # access and refresh tokens
    ret = {
        'access_token': create_access_token(identity=username),
        'refresh_token': create_refresh_token(identity=username)
    }
    return jsonify(ret), 200


# The jwt_refresh_token_required decorator insures a valid refresh
# token is present in the request before calling this endpoint. We
# can use the get_jwt_identity() function to get the identity of
# the refresh token, and use the create_access_token() function again
# to make a new access token for this identity.
@app.route('/refresh', methods=['POST'])
@jwt_refresh_token_required
def refresh():
    current_user = get_jwt_identity()
    ret = {
        'access_token': create_access_token(identity=current_user)
    }
    return jsonify(ret), 200


@app.route('/protected', methods=['GET'])
@jwt_required
def protected():
    username = get_jwt_identity()
    return jsonify(logged_in_as=username), 200


if __name__ == '__main__':
    app.run()

【问题讨论】:

  • 如果我想在 Jwt 令牌设置时间内渲染模板,该怎么做?
  • 我的意思是我可以像这样使用return jsonify(ret),200,render_template('home.html') 的代码
  • 你为什么不问一个新问题?这与此处询问的内容无关。
  • 我问,你对此有什么想法吗? stackoverflow.com/questions/59082689/…

标签: python flask flask-jwt-extended


【解决方案1】:

试试

curl -H "Authorization: Bearer $REFRESH" -X POST http://localhost:5000/refresh

【讨论】:

  • 我试过这个并得到一个错误。这实际上是因为我错误地复制了刷新令牌。感谢您的帮助。
【解决方案2】:

对我来说只有这个有效,两者都是必需的 - 发布数据和 Auth 标头。

 curl -X POST -H "Content-Type: Application/json" -H "Authorization: Bearer $REFRESH" -d "{\"refresh_token\":\"$REFRESH\"}" http://localhost:5000/refresh

【讨论】:

    猜你喜欢
    • 2019-06-04
    • 2020-05-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2016-03-05
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多