TypeError:wrapper()在使用@jwt_required时得到了意外的关键字参数'nam'

问题描述 投票:0回答:1

app.py

from flask import Flask,request
from flask_restful import Api , Resource
from flask_jwt import JWT ,jwt_required , current_identity
from security import auntheticate , identity

app = Flask(__name__)
app.secret_key = "sangam"
api = Api(app)
jwt = JWT(app,auntheticate,identity)

items = []

class Item(Resource):   
    @jwt_required
    def get(self,nam):
        item = next(filter(lambda x:x['name'] == nam, items), None)
        return {'item':item} ,200 if item else 404

    def post(self,nam):
        if next(filter(lambda x:x['name'] == nam,items),None):
            return{'message':"an item with name '{}' already exist." .format(nam)}
        data = request.get_json()
        item = {'name':nam,
        'price':data['price']}
        items.append(item)
        return item,201

    def delete(self,nam):
        global items
        items = list(filter(lambda x:x['name'] != nam,items))
        return{'message':'item deleted'}

    def put(self,nam):
        data = request.get_json()
        item = next(filter(lambda x:x['name'] == nam,items),None)
        if item is None:
            item = {'name':nam,'price':data['price']}
            items.append(item)
        else:
            item.update(data)
        return item

class item_list(Resource):
    def get(self):
        return {'itmes':items}

api.add_resource(Item,'/item/<string:nam>')
api.add_resource(item_list,'/items')
app.run(port = 5000 , debug = True)

securtiy.py

from werkzeug.security import safe_str_cmp
from user import User

users = [User(1,'bob','asdf')]

username_mapping = {u.username: u for u in users}
userid_mapping = {u.id: u for u in users}

def auntheticate(username,password):
    user = username_mapping.get(username , None)
    if user and safe_str_cmp(user.password , password):
        return user

def identity(payload):
    user_id = payload['identity']
    return userid_mapping.get(user_id,None)

user.py

class User:
    def __init__(self,_id,username,password):
        self.id = _id
        self.username = username
        self.password = password

((错误消息)

Traceback (most recent call last):
  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
    return original_handler(e)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\_compat.py", line 38, in reraise
    raise value.with_traceback(tb)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask_restful\__init__.py", line 272, in error_router
    return original_handler(e)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\_compat.py", line 38, in reraise
    raise value.with_traceback(tb)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask_restful\__init__.py", line 468, in wrapper
    resp = resource(*args, **kwargs)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask\views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)

  File "C:\Users\SANGAM PRASAD YADAV\.virtualenvs\venv-6bMM9kJA\lib\site-packages\flask_restful\__init__.py", line 583, in dispatch_request
    resp = meth(*args, **kwargs)

TypeError: wrapper() got an unexpected keyword argument 'nam'
python flask python-3.7 flask-restful flask-jwt
1个回答
0
投票

问题似乎出在Item类的get方法的@jwt_required装饰器中。解决方法:在@jwt_required之后添加括号,就像@jwt_required()一样。就我而言,它有效。

© www.soinside.com 2019 - 2024. All rights reserved.