0 Soalan: TypeError: objek 'modul' tidak boleh dipanggil Flask JWT

soalan dicipta di Wed, May 8, 2019 12:00 AM

Saya sedang menjalankan pelayan Flask di mana kita mempunyai laluan untuk mengesahkan token JWT pengguna menggunakan pustaka flask-jwt-extended . Inilah laluan kami seperti:

from flask_jwt_extended import (
    JWTManager, jwt_required, create_access_token, create_refresh_token, get_jwt_identity, jwt_refresh_token_required
)

@app.route('/api/users/validate', methods=['POST'])
def validate_user():

    if not request.json:
        abort(400)

    data = request.get_json()

    if ('id_token' not in data):
        abort(400, "id_token must be a parameter in the JSON")

    decoded_token = auth.verify_id_token(data['id_token'])

    if ('uid' not in decoded_token):
        return json.dumps({'verified': False}), 200
    else:
        user = mongo.db.users.find_one({'_id': decoded_token['uid']})

        if user:
            access_token = create_access_token(identity=user['_id'])
            refresh_token = create_refresh_token(identity=user['_id'])
            return json.dumps({'verified': True, 'access_token': access_token, 'refresh_token': refresh_token}), 200
        else:
            return json.dumps({'verified': False, 'message': 'unable to locate user'}), 401

Atas sebab tertentu, setiap kali saya cuba membuat permintaan pos ke laluan ini, saya mendapat ralat TypeError: 'module' object is not callable untuk fungsi create_access_token. Berikut adalah jejak tindanan:

127.0.0.1 - - [08/May/2019 12:24:17] "POST /api/users/validate HTTP/1.1" 500 -
Traceback (most recent call last):
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/user/Documents/server/src/controllers/auth_controller.py", line 44, in validate_user
    access_token = create_access_token(identity=user['_id')
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask_jwt_extended/utils.py", line 157, in create_access_token
    return jwt_manager._create_access_token(identity, fresh, expires_delta, user_claims)
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask_jwt_extended/jwt_manager.py", line 476, in _create_access_token
    json_encoder=config.json_encoder
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask_jwt_extended/tokens.py", line 77, in encode_access_token
    json_encoder=json_encoder)
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/flask_jwt_extended/tokens.py", line 31, in _encode_jwt
    json_encoder=json_encoder).decode('utf-8')
  File "/Users/user/Documents/server/venv/lib/python3.6/site-packages/jwt/api_jwt.py", line 61, in encode
    cls=json_encoder
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
TypeError: 'module' object is not callable

Saya telah memeriksa ketiga-tiga dok-lampiran-jwt dan create_acess_token adalah, sebenarnya, fungsi dan ketika mencetak create_acess_token ia mencetak bahawa ia berfungsi. Saya telah melihat hampir semua jawatan StackOverflow pada topik ini dan tiada satu pun daripada mereka yang membantu-kebanyakan mereka bercakap tentang kelas yang dijana pengguna, tetapi ini adalah fungsi yang saya menarik dari perpustakaan luaran.

Berikut adalah definisi create_access_token dalam pelipat-jwt-dilanjutkan:

def _create_access_token(self, identity, fresh=False, expires_delta=None, user_claims=None):
    if expires_delta is None:
        expires_delta = config.access_expires

    if user_claims is None:
        user_claims = self._user_claims_callback(identity)

        access_token = encode_access_token(
            identity=self._user_identity_callback(identity),
            secret=self._encode_key_callback(identity),
            algorithm=config.algorithm,
            expires_delta=expires_delta,
            fresh=fresh,
            user_claims=user_claims,
            csrf=config.csrf_protect,
            identity_claim_key=config.identity_claim_key,
            user_claims_key=config.user_claims_key,
            json_encoder=config.json_encoder
        )
        return access_token

Jika ia membantu, kod itu berada dalam Python 3.6.5 dan ia dalam persekitaran maya. Sebarang bantuan akan sangat dihargai!

    
1
  1. Tunjukkan mesej ralat traceback yang penuh, dan tunjukkan definisi create_access_token (mungkin ia import?)
    2019-05-08 16: 18: 52Z
  2. Adakah ralat ini berlaku pada setiap panggilan POST, atau hanya kadang-kadang?
    2019-05-08 16: 20: 57Z
  3. @ JohnGordon setiap panggilan POST
    2019-05-08 16: 21: 33Z
  4. @ JohnGordon dikemaskini untuk memasukkan definisi tindanan, import, dan definisi penuh
    2019-05-08 16: 29: 34Z
  5. Jika masalahnya adalah bahawa create_access_token bukanlah fungsi, traceback kesilapan akan berakhir pada fungsi pandangan anda. Tetapi ia tidak; ia berterusan selama setengah dozen panggilan, jadi ada yang lain yang salah. Ia kelihatan seperti masalahnya dengan item config.json_encoder di _create_access_token. Adakah anda perlu menjalankan skrip konfigurasi, atau mengedit fail konfigurasi, apabila anda memasang modul itu?
    2019-05-08 16: 43: 24Z
0 Jawapan                              0                         
sumber diletakkan di sini