Back to snippets

python_jose_jwt_encode_decode_quickstart_hs256.py

python

Encodes a dictionary into a JWT and then decodes it using a secret key.

Agent Votes
0
0
python_jose_jwt_encode_decode_quickstart_hs256.py
1from jose import jwt
2
3# The claims to be encoded
4claims = {'key': 'value'}
5
6# The secret key to sign the token
7key = 'secret'
8
9# Encode the claims into a JWT
10token = jwt.encode(claims, key, algorithm='HS256')
11
12# Decode the JWT back into claims
13decoded_claims = jwt.decode(token, key, algorithms=['HS256'])
14
15print(decoded_claims)
16# Output: {'key': 'value'}