OAuth2 Client Credentials (mô phỏng)
POST + GET · /api/practice/level3/oauth2/token · Khó
Mô tả bài tập
Mô phỏng đơn giản luồng 'Client Credentials Grant' của OAuth2 - thường dùng cho giao tiếp server-to-server (machine to machine), không có người dùng cụ thể. POST tới /oauth2/token dạng form-urlencoded với grant_type=client_credentials, client_id=demo-client, client_secret=demo-secret. Nhận về access_token, dùng gọi GET /oauth2/resource.
Mục tiêu học được
- Phân biệt OAuth2 Client Credentials với JWT login thông thường (không có username/password người dùng)
- Làm quen với body dạng application/x-www-form-urlencoded thay vì JSON
- Hiểu khái niệm 'scope' trong response token
Lệnh curl
curl -i -X POST "http://testing-api.systemhub.uk/api/practice/level3/oauth2/token" \
-H "X-Practice-Token: YOUR_TOKEN" \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials&client_id=demo-client&client_secret=demo-secret"
Các bước làm trên Postman
- Tạo request POST tới http://testing-api.systemhub.uk/api/practice/level3/oauth2/token
- Tab Body: chọn x-www-form-urlencoded, thêm grant_type, client_id, client_secret
- Gửi, copy access_token từ response
- GET http://testing-api.systemhub.uk/api/practice/level3/oauth2/resource với Authorization: Bearer <access_token>
Đăng nhập để lệnh curl ở trên tự động điền sẵn Practice Token thật của bạn.
🧪 Thử nghiệm trực tiếp
Gọi API ngay tại đây. Nếu bạn đã đăng nhập, tiến độ sẽ được ghi nhận tự động khi gọi đúng như kỳ vọng của bài.
Kết quả
Status: