Error in loading JSON from itsdangerous file TimedJSONSerializer Token

时间:2016-07-11 20:33:37

标签: python authentication token serializer

I am currently working on authenticating my users. I checked out this blog - RESTful Authentication with Flask - and followed its steps to produce a piece of code I believed would serve my purpose. I wanted to use the Timed Json Serializer class for my particular use case. Below, I am creating an object of that class, generating a token and trying to load the data with it.

from itsdangerous import TimedJSONWebSignatureSerializer
user_id = 'fake1'
s = TimedJSONWebSignatureSerializer(parser_app.config['SECRET_KEY'], expires_in=3600)
token = s.dumps({'user_id' : user_id})
print(token)
print (s.loads(token))

I get the following callback:

Traceback (most recent call last):
eyJhbGciOiJIUzI1NiIsImV4cCI6MTQ2ODI3MjU3MSwiaWF0IjoxNDY4MjY4OTcxfQ.eyJ1c2VyX2lkIjoiZmFrZTEifQ.Ch8y6BDMIIBdIGM0lmjdAimINvP3PnUmBpOp-jDW18w
  File "C:/Users/vaibhav/PycharmProjects/Coding/Coding.py", line 6, in <module>
    print (s.loads(token))
  File "C:\Users\vaibhav\Anaconda\lib\site-packages\itsdangerous.py", line 798, in loads
    self, s, salt, return_header=True)
  File "C:\Users\vaibhav\Anaconda\lib\site-packages\itsdangerous.py", line 752, in loads
    self.make_signer(salt, self.algorithm).unsign(want_bytes(s)),
  File "C:\Users\vaibhav\Anaconda\lib\site-packages\itsdangerous.py", line 377, in unsign
    payload=value)
itsdangerous.BadSignature: Signature 'Ch8y6BDMIIBdIGM0lmjdAimINvP3PnUmBpOp-jDW18w' does not match

I have just created the token with an expiry of an hour and it gives me a BadSignature which indicates that the token does not match. The desired output would be:

{"user_id" : "fake1"}

Please help me out.

1 个答案:

答案 0 :(得分:0)

I ended up having to uninstall and reinstall the package itsdangerous with pip. The statements used were:

pip uninstall itsdangerous

followed by:

pip install itsdangerous

Apparently, the file had been corrupted somehow causing it not to work properly.