当启用身份验证时,Flask-restful应用程序失败



每当我尝试使用flask_httpauth为我的flask_restful项目启用身份验证时,我都会得到此错误:

AttributeError: 'function' object has no attribute 'as_view'

下面是一个非常基本的例子:apicontroller.py:

from flask_restful import Resource, Api
from flasktest import api, app
from flask_httpauth import HTTPTokenAuth
auth = HTTPTokenAuth()
@auth.login_required
class ApiController(Resource):
    def get(self):
        return True
api.add_resource(ApiController, '/api/apicontroller')

init . py:

from flask import render_template
from flask import Flask, request, render_template, session, flash, redirect, url_for, jsonify
from flask_restful import Resource, Api, reqparse, fields
from flask_httpauth import HTTPTokenAuth

app = Flask(__name__)
api = Api(app)
import flasktest.apicontroller

每当我用@auth.login_required装饰控制器类时,它就会失败,并出现上述错误。我该如何解决这个问题?

我相信你不能在类中不应用装饰符。

解决方法:

from flask_restful import Resource, Api
from flasktest import api, app
from flask_httpauth import HTTPTokenAuth
auth = HTTPTokenAuth()
class ApiController(Resource):
    decorators = [auth.login_required]
    def get(self):
        return True
api.add_resource(ApiController, '/api/apicontroller')

相关内容

  • 没有找到相关文章

最新更新