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

时间:2016-07-10 15:50:12

标签: python flask flask-httpauth

我一直在使用Miguel Grinberg的一些auth教程,并使用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文件(从中导入auth,app,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'

1 个答案:

答案 0 :(得分:3)

改变这个:

auth = HTTPBasicAuth

到此:

auth = HTTPBasicAuth()