简体   繁体   English

flask-jwt-extended TypeError:类型为'function'的对象不可JSON序列化

[英]flask-jwt-extended TypeError: Object of type 'function' is not JSON serializable

I'm trying to make an API jwt authenticate with flask using flask_sqlalchemy and flask_jwt_extended,marshmallow to validate input data Login route to get token: 我正在尝试使用flask_sqlalchemy和flask_jwt_extended使棉花糖的API jwt进行身份验证,棉花糖来验证输入数据以获取令牌的登录路径:

from flask import Flask,request,jsonify, abort
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
from models import Book, Publisher, User
from crud import session
from config import DATABASE_URL
from marshmallow import Schema, fields, validates, ValidationError
from marshmallow.validate import Length, Range
from flask_jwt_extended import (
    JWTManager, jwt_required, create_access_token,
    get_jwt_identity
)
import os
app = Flask(__name__)
app.config['JWT_SECRET_KEY'] = '1234'
jwt = JWTManager(app)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
#init db
db = SQLAlchemy(app)
#init ma
ma = Marshmallow(app)
@app.route('/login', methods=['POST'])
def login():
  if not request.is_json:
    return jsonify({"msg": "Missing JSON in request"}), 400
  username = request.json['username']
  password = request.json['password']
  if not username:
    return jsonify({"msg": "Missing username"}), 400
  if not password:
    return jsonify({"msg": "Missing password"}), 400
  if not db.session.query(User).filter(User.username == username, User.password == password).first():
    return jsonify({"msg": "Wrong username or password"}), 401
  access_token = create_access_token(identity=username)
  return jsonify(access_token=create_access_token), 200
#protected route 
@app.route('/book', methods=['POST'])
@jwt_required
def add_book():
  id = request.json['id']
  title = request.json['title']
  author = request.json['author']
  publisher_id = request.json['publisher_id']
  price = request.json['price']
  pages = request.json['pages']
  published = request.json['published']
  errors = create_book_schema.validate(request.get_json())
  if errors:
    return jsonify(msg=errors), 400
  new_book = Book(id=id,title=title,author=author,publisher_id=publisher_id,price=price,pages=pages,published=published)
  db.session.add(new_book)
  db.session.commit()
  return jsonify(msg='book successfully created', book_id=new_book.id), 200

When i send json input through postman at "/login" route to get the token i got this error return : 当我通过邮递员在“ / login”路由上发送json输入以获取令牌时,出现以下错误返回:

TypeError: Object of type 'function' is not JSON serializable TypeError:“功能”类型的对象不可JSON序列化

What am i doing wrong ? 我究竟做错了什么 ? Thank 谢谢

EDIT Here the input json which is the same as in my database 编辑这里输入的json与我的数据库中的相同

{
    "username": "linh",
    "password": "123"
}

You need to return a serializable object for jsonify to work: 您需要返回一个可序列化的对象,以便jsonify起作用:

return jsonify(dict(access_token=access_token)), 200

You already have your access_token defined in a variable, you can just return it inside of a dictionary. 您已经在变量中定义了access_token ,您可以将其返回到字典中。

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

相关问题 TypeError: Object of type function is not JSON serializable when using flask_jwt_extended int RESTful API - TypeError: Object of type function is not JSON serializable when using flask_jwt_extended int RESTful API Python/Flask - “TypeError”类型的对象不是 JSON 可序列化的 - Python/Flask - Object of type "TypeError' is not JSON serializable Python - Flask - TypeError 类型的 Object 不是 JSON 可序列化的 - Python - Flask - Object of type TypeError is not JSON serializable Json jwt 令牌问题 - TypeError: Object of type 'User' is not Z0ECD11C1D7A2874021D1Z8A23DBB1 - Json jwt token problem - TypeError: Object of type 'User' is not JSON serializable TypeError:类型函数的对象不可JSON序列化 - TypeError: Object of type function is not JSON serializable TypeError: Object 类型 function 不是 JSON 在 Z2B9AFB89A38ACC1575B159 中可序列化的 - TypeError: Object of type function is not JSON serializable in django get_jwt_identity() 在 Flask-JWT-Extended 中返回 None - get_jwt_identity() returning None in Flask-JWT-Extended 使用 Flask-JWT-Extended 和 Flask-restx - Using Flask-JWT-Extended with Flask-restx 在使用 Flask-JWT-Extended 和 Flask-Restful 时遇到问题 - Having issues using Flask-JWT-Extended with Flask-Restful 如何解决 Flask 中的版本冲突错误(PyJWT 和 Flask-JWT-Extended) - How to resolve versionConflict error in Flask (PyJWT and Flask-JWT-Extended)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM