Create a JWT request token

This simple recipe demonstrates how to encode a JWT request token and output it. The output can be used as the string to pass in the jwt params in the HTTP POST to /auth/token

1

๐Ÿ› ๏ธ Enter Config Info

Fill out with values for the API key you already created.

# ๐Ÿ“ข Netography API 2023: ๐Ÿ” JWT Token Encoding
#
# Instructions:
# 1. Install the PyJWT package using pip: `pip install PyJWT`
# 2. Add your values to the config_json below.
# 3. Run the script to generate and print the encoded JWT token.

import jwt
import random
import time
import json

config_json = '''
{
  "APPNAME": "",       
  "APPKEY": "",          
  "SHORTNAME": "",       
  "SHARED_SECRET": ""   
}
'''
2

๐Ÿ“ฆ Prepare JWT Payload

Set up the payload with necessary information for creating the JWT token.

config = json.loads(config_json)

payload = {
    'iat': int(time.time()),
    'jti': random.randint(0,10000000),
    'appname': config['APPNAME'],
    'appkey': config['APPKEY'],
    'shortname': config['SHORTNAME']
}
3

๐Ÿ” Encode JWT Token and Print

Encode the payload with the shared secret using the PyJWT library, and print the encoded JWT token as a string to stdout.

token = jwt.encode(payload, config['SHARED_SECRET'], algorithm="HS256")
print(token)

Last updated