简体   繁体   English

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

[英]flask_httpauth decorators missing required positional argument f

I've been working with some of Miguel Grinberg's auth tutorials and have come across an issue using flask_httpauth's HTTPBasicAuth decorators. 我一直在与Miguel Grinberg的auth教程合作,并且使用flask_httpauth的HTTPBasicAuth装饰器遇到了一个问题。 Whenever I use one of them on a function I get an error stating that the decorator is missing required positional argument f. 每当我在函数上使用它们之一时,都会收到错误消息,指出装饰器缺少必需的位置参数f。 It was my understanding that the function beneath the decorator was implicitly passed to the decorator function as an argument. 据我了解,装饰器下面的函数作为参数被隐式传递给装饰器函数。 Am I wrong? 我错了吗? I'm using Python 3.5. 我正在使用Python 3.5。

My views file looks like this: 我的视图文件如下所示:

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})

and my init file (where auth, app, api, etc are imported from) looks like this: 我的初始化文件(从中导入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

Any idea why this isn't working for me? 知道为什么这对我不起作用吗? The exact error runs like this: 确切的错误是这样运行的:

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

Change this: 更改此:

auth = HTTPBasicAuth

to this: 对此:

auth = HTTPBasicAuth()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM