FlaskRestx和Swagger授权在标头中发送不正确的令牌

2024-10-06 12:27:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个Flask REST API,它使用Flask Restx和JWT令牌身份验证,并且正在进行postman调用。但是,当我尝试使用swagger时,发送的令牌不是我通过接口输入的令牌。我的代码如下所示:

blueprint = Blueprint('api_bp', __name__, url_prefix='/api/1')

authorizations = {
    'api_key' : {
        'type' : 'apiKey',
        'in' : 'header',
        'name' : 'x-access-token'
    }
}

api = Api(blueprint,
          authorizations=authorizations,
          title='My Title',
          version='1.0',
          security='api_key'
         )
from timesheets.views.api_bp import api as ns1

np = api.namespace('index', description='Index API')

def token_required(f): 
@wraps(f)
    def decorated(*args, **kwargs):
...

@np.route('/users')
class Users(Resource):
    @api.doc(security='api_key')
    @token_required
    def get(self, current_user):
        ...
        return jsonify({'user' : output})

然后在“大摇大摆”页面上,我可以输入我的身份验证令牌: enter image description here

当我“试用”时,我可以看到正确的x-access-token被放置在curl调用中

enter image description here

但是,如果我查看我的请求头,每次我获得发送到服务器的相同x-access-token时:

enter image description here

那么,这个令牌是从哪里生成的呢?我如何确保我只使用通过接口传递的令牌


Tags: keynametoken身份验证apiflaskaccessdef