Python 3.x pyjwt引发类型错误
我使用pyjwt的方式如下:Python 3.x pyjwt引发类型错误,python-3.x,unit-testing,pyjwt,Python 3.x,Unit Testing,Pyjwt,我使用pyjwt的方式如下: def encode_auth_token(self, user_id): '''Generates the auth token.''' try: payload = { 'exp': datetime.utcnow() + datetime.timedelta( days = current_app.config.get('TOKEN_EXPIRATION_DAYS'),
def encode_auth_token(self, user_id):
'''Generates the auth token.'''
try:
payload = {
'exp': datetime.utcnow() + datetime.timedelta(
days = current_app.config.get('TOKEN_EXPIRATION_DAYS'),
seconds = current_app.config.get('TOKEN_EXPIRATION_SECONDS')
),
'iat': datetime.datetime.utcnow(),
'sub': user_id
}
return jwt.encode(
payload,
current_app.config.get('SECRET_KEY'),
algorithm='HS256'
)
except Exception as e:
return e
问题是,根据docs instance.encode()应该返回字节
,根据另一个资源,它应该返回str
。当我通过单元测试运行它时:
auth_token = user.encode_auth_token(user.id)
self.assertTrue(isinstance(auth_token, str))
我得到:
AssertionError:False不是真的
当我用bytes
替换str
时,我得到了相同的错误。那么这个方法应该返回什么类型呢?它通常返回字节数据。如果可以确认它返回字符串,则可以通过调用令牌实例本身上的decode方法强制它返回字符串
token=jwt.encode(有效载荷,秘密)。解码('utf-8'))
返回令牌