【问题标题】:Error 401 in Swagger UI for Flask Restplus APIFlask Restplus API 的 Swagger UI 中出现错误 401
【发布时间】:2019-02-20 16:09:44
【问题描述】:

我正在使用flask_restplusflask_httpauth 构建一个API,用于基于令牌的身份验证。一切都很好,除了一件事。在 Swagger UI 中,当我尝试执行搜索查询时收到错误 401。在授权中添加“承载”没有帮助。请看下面的截图。

有趣的是,从curl(在 Swagger UI 中)复制和粘贴输出并从终端运行它确实会返回正确的输出。与在 Postman 中运行它相同。问题可能出在 Swagger UI 中。有什么建议吗?

不幸的是,Github 上的建议对我不起作用。

注意:verify_token(token) 似乎没有从 Swagger UI 接收输入。当我尝试打印 token 时,从 Swagger 调用时为空,但从 curl/Postman 调用时显示值。

部分代码:

authorizations = {
    'Bearer Auth': {
        'type': 'apiKey',
        'in': 'header',
        'name': 'Authorization'
    },
}

api = Api(blueprint, version='1.0', title='Flask API',
          description='My API', security='Bearer Auth', authorizations=authorizations)

@token_auth.verify_token
def verify_token(token):
    g.current_user = User.check_token(token) if token else None
    return g.current_user is not None

【问题讨论】:

  • 您在哪里添加您的不记名令牌?
  • 通过“授权”按钮(在屏幕截图中不可见,但右上角的锁表示它)。在“区域名称”下添加一个字段也是可能的,但没有帮助......
  • 在 swagger 上请求之前,我没有尝试过登录这个功能,但我仍然认为它与格式错误/错误的不记名令牌有关..
  • 是的,我也是。卷曲确实可以正常工作
  • 在使用 Swagger 时,“token”保持为空

标签: python swagger-ui flask-restplus


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2019-07-16
  • 2019-11-04
  • 1970-01-01
  • 1970-01-01
  • 2012-07-19
  • 2020-06-10
  • 2020-01-08
  • 1970-01-01
相关资源
最近更新 更多