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": ""
}
'''// ๐ข Netography API 2023: ๐ JWT Token Encoding
//
// Instructions:
// 1. Install Node.js and npm (https://nodejs.org/)
// 2. Install the jsonwebtoken package using npm: `npm install jsonwebtoken`
// 3. Add your values to the configJson below.
// 4. Run the script to generate and print the encoded JWT token.
const jwt = require("jsonwebtoken");
const configJson = `
{
"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']
}const config = JSON.parse(configJson);
const payload = {
iat: Math.floor(Date.now() / 1000),
jti: Math.floor(Math.random() * 10000000),
appname: config.APPNAME,
appkey: config.APPKEY,
shortname: config.SHORTNAME
};payload = {
'iat' => Time.now.to_i,
'jti' => rand(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)const token = jwt.sign(payload, config.SHARED_SECRET, { algorithm: "HS256" });
console.log(token);token = JWT.encode(payload, config['SHARED_SECRET'], 'HS256')
puts token const token = jwt.sign(payload, config.SHARED_SECRET, { algorithm: "HS256" });
return {
statusCode: 200,
body: token,
};
};Last updated