Python · API · Intermediate
Token Authentication
Register a user, obtain an auth token, and make authenticated API requests.
Quick topic start and explanations before exercises (exercises below):
Registration, token, and authenticated requests
#Token auth reference
#Exercises:
Register a new user
#Register a new user by sending a POST to /api/users/register/ with a username and password. Print the status code and the response JSON.
import requests
BASE_URL = 'https://apilearn.tukas.dev'
data = {
'username': 'testuser01',
'password': 'mypassword123',
}
# Register and print status code + response
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
data = {
'username': 'testuser01',
'password': 'mypassword123',
}
response = requests.post(f'{BASE_URL}/api/users/register/', json=data)
print(response.status_code)
print(response.json())
Get an auth token
#POST to /api/auth/token/ with your username and password and print the token string from the response.
import requests
BASE_URL = 'https://apilearn.tukas.dev'
credentials = {
'username': 'testuser01',
'password': 'mypassword123',
}
# Get token and print it
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
credentials = {
'username': 'testuser01',
'password': 'mypassword123',
}
response = requests.post(f'{BASE_URL}/api/auth/token/', json=credentials)
token = response.json()['token']
print(token)
Access a protected endpoint
#Using the token you obtained, send a GET to /api/users/profile/ with the Authorization header set. Print the username from the profile response.
import requests BASE_URL = 'https://apilearn.tukas.dev' token = 'PASTE_YOUR_TOKEN_HERE' # GET profile with Authorization header and print username
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
token = 'PASTE_YOUR_TOKEN_HERE'
headers = {'Authorization': f'Token {token}'}
response = requests.get(f'{BASE_URL}/api/users/profile/', headers=headers)
print(response.json()['username'])
See what happens without auth
#Try to GET /api/cart/ without any Authorization header. Print the status code and the error message from the response.
import requests BASE_URL = 'https://apilearn.tukas.dev' # GET /api/cart/ with no auth header
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
response = requests.get(f'{BASE_URL}/api/cart/')
print(response.status_code)
print(response.json())
Use a Session for multiple calls
#Register a user, get a token, then use requests.Session() to make two authenticated calls — GET /api/users/profile/ and GET /api/cart/ — without specifying the Authorization header on each call. Print the username from the profile and the cart response.
import requests BASE_URL = 'https://apilearn.tukas.dev' # 1. Register # 2. Get token # 3. Create session with auth header # 4. Make two calls using the session
Solution
import requests
BASE_URL = 'https://apilearn.tukas.dev'
# Register
reg = requests.post(f'{BASE_URL}/api/users/register/', json={
'username': 'sessionuser01', 'password': 'securepass123',
})
# Get token
token = requests.post(f'{BASE_URL}/api/auth/token/', json={
'username': 'sessionuser01', 'password': 'securepass123',
}).json()['token']
# Session
session = requests.Session()
session.headers['Authorization'] = f'Token {token}'
profile = session.get(f'{BASE_URL}/api/users/profile/').json()
cart = session.get(f'{BASE_URL}/api/cart/').json()
print(profile['username'])
print(cart)