Python 当令牌授权失败时,从connexion flask服务器发送自定义响应

Python 当令牌授权失败时,从connexion flask服务器发送自定义响应,python,flask,oauth-2.0,authorization,connexion,Python,Flask,Oauth 2.0,Authorization,Connexion,在命中每个端点之前,对令牌进行授权。我已经创建了应用程序,如下所示 os.environ[TOKENINFO\u FUNC]=一些函数 add.api(openapi.yaml) app.add_error_处理程序(OAuthResponseProblem,render_unauthorized) def render_未经授权: 返回自定义响应 定义某些_函数: 提出OAuthResponseProblem 这是挂接异常的正确方法吗?我收到上述代码的错误。此处未调用render_unaut

在命中每个端点之前,对令牌进行授权。我已经创建了应用程序,如下所示

os.environ[TOKENINFO\u FUNC]=一些函数
add.api(openapi.yaml)
app.add_error_处理程序(OAuthResponseProblem,render_unauthorized)
def render_未经授权:
返回自定义响应
定义某些_函数:
提出OAuthResponseProblem

这是挂接异常的正确方法吗?我收到上述代码的错误。此处未调用render_unauthorized。我想验证令牌并发送端点用户自定义响应。提前感谢。

您需要实现
x-tokenInfoFunc
。在您的函数中,您可以发送自定义响应

  • 文件:
  • 例如:

    • 如果太晚了,我很抱歉。我也面临同样的问题。您需要做的就是使用自定义字符串引发未经授权的异常

      import jwt    
      from werkzeug.exceptions import Unauthorized    
      
      # This is your TOKENINFO_FUNC
      def decode_token(jwt_token):
        try:
          payload = jwt.decode(jwt_token, 'SECRET_KEY', algorithms=['HS256'])
          return payload
        except jwt.ExpiredSignatureError:
          raise Unauthorized('Token expired. Please log in again.')
        except jwt.InvalidTokenError:
          raise Unauthorized('Invalid token. Please log in again.')