Python · API · Intermediate
JWT Authentication
Use JSON Web Tokens for authentication, understand access/refresh token flow, and handle token refresh.
Quick topic start and explanations before exercises (exercises below):
Getting and refreshing JWT tokens
#JWT reference
#Exercises:
Get a JWT token pair
#POST to /api/auth/jwt/ with your username and password. Print the first 30 characters of both the access and refresh tokens.
import requests
BASE_URL = 'https://apilearn.tukas.dev'
credentials = {'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD'}
# Get JWT pair and print the first 30 chars of each token
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
credentials = {'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD'}
response = requests.post(f'{BASE_URL}/api/auth/jwt/', json=credentials)
tokens = response.json()
print(tokens['access'][:30])
print(tokens['refresh'][:30])
Use Bearer auth
#Get a JWT access token and use it with the Bearer scheme to GET /api/users/profile/. Print the username and email from the response.
import requests BASE_URL = 'https://apilearn.tukas.dev' # Get access token, then GET profile with Authorization: Bearer <token>
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
r = requests.post(f'{BASE_URL}/api/auth/jwt/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
})
access = r.json()['access']
headers = {'Authorization': f'Bearer {access}'}
profile = requests.get(f'{BASE_URL}/api/users/profile/', headers=headers).json()
print(profile['username'])
print(profile['email'])
Refresh an access token
#Get a JWT pair, then immediately use the refresh token to get a new access token via POST /api/auth/jwt/refresh/. Print the new access token (first 30 characters).
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Get initial token pair # 2. Use refresh token to get a new access token # 3. Print the first 30 chars of the new access token
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
r = requests.post(f'{BASE_URL}/api/auth/jwt/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
})
refresh = r.json()['refresh']
r2 = requests.post(f'{BASE_URL}/api/auth/jwt/refresh/', json={'refresh': refresh})
new_access = r2.json()['access']
print(new_access[:30])
Write get_access_token()
#Write a function get_access_token(username, password) that returns a fresh JWT access token. Call it and print the result.
import requests
BASE_URL = 'https://apilearn.tukas.dev'
def get_access_token(username, password):
# POST to /api/auth/jwt/ and return the access token
pass
token = get_access_token('YOUR_USERNAME', 'YOUR_PASSWORD')
print(token[:30])
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
def get_access_token(username, password):
response = requests.post(
f'{BASE_URL}/api/auth/jwt/',
json={'username': username, 'password': password},
)
return response.json()['access']
token = get_access_token('YOUR_USERNAME', 'YOUR_PASSWORD')
print(token[:30])
Write refresh_access()
#Write a function refresh_access(refresh_token) that returns a new access token using the refresh endpoint. Then write a small script that gets the initial pair and immediately refreshes the access token, printing both the original and new access tokens (first 20 characters each).
import requests
BASE_URL = 'https://apilearn.tukas.dev'
def refresh_access(refresh_token):
# POST to /api/auth/jwt/refresh/ and return the new access token
pass
# Get initial pair, then refresh
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
def refresh_access(refresh_token):
response = requests.post(
f'{BASE_URL}/api/auth/jwt/refresh/',
json={'refresh': refresh_token},
)
return response.json()['access']
r = requests.post(f'{BASE_URL}/api/auth/jwt/', json={
'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD',
})
original_access = r.json()['access']
refresh_token = r.json()['refresh']
new_access = refresh_access(refresh_token)
print('original:', original_access[:20])
print('refreshed:', new_access[:20])