JWT:“模块"对象没有属性“编码" [英] JWT: 'module' object has no attribute 'encode'

查看:635
本文介绍了JWT:“模块"对象没有属性“编码"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 jwt 找不到模块错误 >.这是我的声明方式:

I am getting a Module not found error when using jwt. Here is how I declared it:

def create_jwt_token():
    payload = {
        "iat": int(time.time())
    }

    shared_key = REST_API_TOKEN
    payload['email'] = EMAIL
    payload['password'] = PASSWORD

    jwt_string = jwt.encode(payload, shared_key)
    encoded_jwt = urllib.quote_plus(jwt_string)  # URL encode the JWT string

    return encoded_jwt

错误消息说在jwt中找不到编码.我在jwt上做了一个选项卡,发现encoding是jwt.JWT内部的一种方法.我尝试将其更改为

The error message says encode is not found in jwt. I did a tab on jwt and found that the encode is a method inside jwt.JWT. I tried changing it to

jwt_string = jwt.JWT.encode(payload, shared_key)

并显示此错误:

必须以JWT实例作为第一个参数(而不是dict实例)调用未绑定方法encode()

unbound method encode() must be called with JWT instance as first argument (got dict instance instead)

我做错了什么?这是我的Python环境的版本信息:

What am I doing it wrong? Here is the version information of my Python environment:

2.7.10 | Anaconda 2.3.0(64位)| (默认值,2015年5月28日,16:44:52)[MSC v.1500 64位(AMD64)]

2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)]

推荐答案

如果同时安装了JWT和PyJWT,则会出现问题.在执行import jwt时,它将导入库JWT而不是PyJWT-后者是您要编码的库.我做了pip uninstall JWTpip uninstall PyJWT然后是pip install PyJWT.之后,它导入了正确的模块并生成了令牌! :)

The problem arises if you have both JWT and PyJWT installed. When doing import jwt it is importing the library JWT as opposed to PyJWT - the latter is the one you want for encoding. I did pip uninstall JWT and pip uninstall PyJWT then finally pip install PyJWT. After that it imported the correct module and generated the token! :)

这篇关于JWT:“模块"对象没有属性“编码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆