the dumps method of itsdangerous throws a TypeError

2024/10/13 3:29:23

I am following the guide of 『Flask Web Development』.
I want to use itsdangerous to generate a token, but some problems occured. Here is my code:

def generate_confirmation_token(self, expiration=3600):s = Serializer(current_app.config['SECRET_KEY'], expiration)return s.dumps({'confirm': self.id})

The self.id is an int object.

But unfortunately,the "dumps" method throws a TypeError:

  File "/Users/zzx/projects/PycharmProjects/wurong/app/models.py", line 76, in generate_confirmation_tokenreturn s.dumps({'confirm': self.id})File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 566, in dumpsrv = self.make_signer(salt).sign(payload)File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 353, in signreturn value + want_bytes(self.sep) + self.get_signature(value)File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 347, in get_signaturekey = self.derive_key()File "/Users/zzx/.pyenv/versions/3.6.1/envs/venv_blog/lib/python3.6/site-packages/itsdangerous.py", line 333, in derive_keyreturn self.digest_method(salt + b'signer' +
TypeError: unsupported operand type(s) for +: 'int' and 'bytes'

I don't know why this problem occurred, I just follow the guide of my book~

Answer

The second argument to Serializer is salt, not expiration. expiration is not an argument to Serializer at all, it's an argument to TimedSerializer.loads called max_age.

If you want a token that expires, use TimedSerializer and pass the expiration when you load the token, not when you create it.

def generate_confirmation(self):s = TimedSerializer(current_app.secret_key, 'confirmation')return s.dumps(self.id)def check_confirmation(self, token, max_age=3600):s = TimedSerializer(current_app.secret_key, 'confirmation')return s.loads(token, max_age=max_age) == self.id
https://en.xdnf.cn/q/118128.html

Related Q&A

SP 500 List python script crashes

So I have been following a youtube tutorial on Python finance and since Yahoo has now closed its doors to the financial market, it has caused a few dwelling problems. I run this codeimport bs4 as bs im…

sleekxmpp threaded authentication

so... I have a simple chat client like so:class ChatClient(sleekxmpp.ClientXMPP):def __init__(self, jid, password, server):sleekxmpp.ClientXMPP.__init__(self, jid, password, ssl=True)self.add_event_han…

DoxyPy - Member (variable) of namespace is not documented

I get the error message warning: Member constant1 (variable) of namespace <file_name> is not documented. for my doxygen (doxypy) documentation. I have documented the file and all functions and cl…

to_csv append mode is not appending to next new line

I have a csv called test.csv that looks like:accuracy threshold trainingLabels abc 0.506 15000 eew 18.12 15000And then a dataframe called summaryDF that looks like:accu…

Optional keys in string formats using % operator?

Is is possible to have optional keys in string formats using % operator? I’m using the logging API with Python 2.7, so I cant use Advanced String Formatting.My problem is as follow:>>> impor…

HDF5 headers missing in installation of netCDF4 module for Python

I have attempted to install the netCDF4 module several times now and I keep getting the same error:Traceback (most recent call last):File "<string>", line 17, in <module>File &quo…

How to catch network failures while invoking get() method through Selenium and Python?

I am using Chrome with selenium and the test run well, until suddenly internet/proxy connection is down, then browser.get(url) get me this:If I reload the page 99% it will load fine, what is the proper…

Pandas crosstab with own function

I have a function which takes two inputs and returns a float e.g. my_func(A, B) = 0.5. I have a list of possible inputs: x = [A, B, C, D, E, F].I want to produce a square matrix (in this case 6 by 6) …

Using a custom threshold value with tf.contrib.learn.DNNClassifier?

Im working on a binary classification problem and Im using the tf.contrib.learn.DNNClassifier class within TensorFlow. When invoking this estimator for only 2 classes, it uses a threshold value of 0.5 …

How to remove certain characters from a variable? (Python)

Lets suppose I have a variable called data. This data variable has all this data and I need to remove certain parts of it while keeping most of it. Lets say I needed to remove all the , (commas) in thi…