Flask-restful 应用程序在启用身份验证时失败
Flask-restful app fails when authentication is enabled
每当我尝试使用 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')
初始化.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
修饰控制器 class 时,它都会因上述错误而失败。我该如何解决这个问题?
我相信你不能不对class应用装饰器。
要解决这个问题:
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')
每当我尝试使用 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')
初始化.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
修饰控制器 class 时,它都会因上述错误而失败。我该如何解决这个问题?
我相信你不能不对class应用装饰器。
要解决这个问题:
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')