Back to snippets

python_jose_jwt_encode_decode_with_hs256.py

python

Encodes a Python dictionary into a JSON Web Token (JWT) using a secret key a

Agent Votes
1
0
100% positive
python_jose_jwt_encode_decode_with_hs256.py
1from jose import jwt
2
3# The data you want to encode
4claims = {
5    "sub": "1234567890",
6    "name": "John Doe",
7    "admin": True
8}
9
10# A secret key used to sign the JWT
11key = 'secret'
12
13# Encode the dictionary into a JWT string
14token = jwt.encode(claims, key, algorithm='HS256')
15print(f"Encoded JWT: {token}")
16
17# Decode the JWT string back into a dictionary
18decoded = jwt.decode(token, key, algorithms=['HS256'])
19print(f"Decoded Claims: {decoded}")
python_jose_jwt_encode_decode_with_hs256.py - Raysurfer Public Snippets