flask_httpauth装饰器缺少所需的位置参数 f



我一直在使用Miguel Grinberg的一些身份验证教程,并且在使用flask_httpauth的HTTPBasicAuth装饰器时遇到了一个问题。每当我在函数上使用其中一个时,我都会收到一个错误,指出装饰器缺少所需的位置参数 f。我的理解是,装饰器下面的函数作为参数隐式传递给装饰器函数。我错了吗?我正在使用Python 3.5。

我的视图文件如下所示:

from mymodule import app, api, auth
from flask import abort, request, jsonify, g, url_for
from mymodule.users.models import User

@auth.verify_password
def verify_password(username_or_token, password):
    # first try to authenticate by token
    user = User.verify_auth_token(username_or_token)
    if not user:
        # try to authenticate with username/password
        user = User.query.filter_by(username=username_or_token).first()
        if not user or not user.verify_password(password):
            return False
    g.user = user
    return True

@app.route('/api/users', methods=['POST'])
def new_user():
    username = request.json.get('username')
    password = request.json.get('password')
    if username is None or password is None:
        abort(400)    # missing arguments
    if User.query.filter_by(username=username).first() is not None:
        abort(400)    # existing user
    user = User(username=username)
    user.hash_password(password)
    db.session.add(user)
    db.session.commit()
    return (jsonify({'username': user.username}), 201,
            {'Location': url_for('get_user', id=user.id, _external=True)})

@app.route('/api/users/<int:id>')
def get_user(id):
    user = User.query.get(id)
    if not user:
        abort(400)
    return jsonify({'username': user.username})

@app.route('/api/token')
@auth.login_required
def get_auth_token():
    token = g.user.generate_auth_token(600)
    return jsonify({'token': token.decode('ascii'), 'duration': 600})

@app.route('/')
@auth.login_required
def index():
    return "Hello, %s!" % g.current_user

@app.route('/api/resource')
@auth.login_required
def get_resource():
    return jsonify({'data': 'Hello, %s!' % g.user.username})

我的 init 文件(从中导入身份验证、应用程序、API 等)如下所示:

import logging
from logging.handlers import RotatingFileHandler
from flask_sqlalchemy import SQLAlchemy
from flask_httpauth import HTTPTokenAuth, HTTPBasicAuth
from flask_restful import Api
from itsdangerous import TimedJSONWebSignatureSerializer as JWT
from flask import Flask
app = Flask(__name__)
"""
Database Config
"""
app.config['SQLALCHEMY_DATABASE_URL'] = 'sqlite:////db.sqlite'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SECRET_KEY'] = 'top secret!'
handler = RotatingFileHandler('logfile.log', maxBytes=10000, backupCount=1)
handler.setLevel(logging.INFO)
app.logger.addHandler(handler)
api = Api(app)
db = SQLAlchemy(app)
auth = HTTPBasicAuth
jwt = JWT(app.config['SECRET_KEY'], expires_in=3600)
import mymodule.users.views

知道为什么这对我不起作用吗?确切的错误如下所示:

File "/.../users/views.py", line 14, in <module>
    @auth.verify_password
TypeError: verify_password() missing 1 required positional argument: 'f'

更改以下内容:

auth = HTTPBasicAuth

对此:

auth = HTTPBasicAuth()

相关内容

  • 没有找到相关文章

最新更新