用于FlaskJWT认证的Apidoc

2024-04-19 21:54:32 发布

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

我已经开始使用apidoc来生成restapi文档。我试过用它。在

在我基于flask的api应用程序中,我使用JWT(尤其是flask jwt)进行身份验证。doc得到了正确的生成,但是来自doc的请求没有正确地发生。在

我在这里放置了生成doc的docsting

def authenticate(username, password):
    """
    @api {post} /auth Authentication url
    @apiName Authentication
    @apiGroup Login

    @apiParam {String} username Username of the user
    @apiParam {String} password Password of the user

    @apiSuccess {String} access_code JWT

    @apiSuccessExample Success Response
    HTTP/1.1 200 OK
    {
      "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZGVudGl0eSI6IjM5MDA4MGExLWY0ZjctMTFlNS04NTRkLTI4ZDI0NDQyZDNlNyIsImlhdCI6MTQ1OTE3ODE0NSwibmJmIjoxNDU5MTc4MTQ1LCJleHAiOjE0NTkxNzg0NDV9.nx_1a4RmvJ7Vlf1CvnMzqoTfzChcuJnDb1Tjy1_FnXw"
    }

    @apiErrorExample Invalid Credentials
    {
      "description": "Invalid credentials",
      "error": "Bad Request",
      "status_code": 401
    }
    """
    user = session.query(User).filter_by(username=username).first()
    if user and user.is_correct_password(password):
        return user
    return False

当我生成文档时,它得到了正确的生成

^{pr2}$
  1. 但是当我从生成的文档传递凭据时,请求失败。我在flask的请求中得到的请求数据是''(空)
  2. 即使在docstring中包含了Content-Type头,它也会失败。我在flask的请求中得到的请求数据是username=test@example.com&password=test123

我已经通过在Flask JWT的代码中添加调试点来确认上述内容

def _default_auth_request_handler():
    data = request.get_json()   # My debug poing
    username = data.get(current_app.config.get('JWT_AUTH_USERNAME_KEY'), None)
    password = data.get(current_app.config.get('JWT_AUTH_PASSWORD_KEY'), None)

但是当我使用postman发出同样的请求时,它会正常地发生。在

邮递员邮件头 enter image description here

Apidoc标题 enter image description here

我错过了什么?在


Tags: 文档authapiflaskdatagetstringauthentication