1. Đăng ký OAuth Application
Đầu tiên, bạn cần tạo OAuth Application để nhận Client ID và Client Secret.
2. Redirect người dùng đến trang authorize
Khi người dùng muốn đăng nhập, redirect họ đến:
http://localhost:3000/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=YOUR_CALLBACK_URL&scope=profile%20email&state=RANDOM_STRINGParameters:
client_id- Client ID của ứng dụngredirect_uri- URL callback đã đăng kýscope- Quyền truy cập (cách nhau bởi space)state- String ngẫu nhiên để bảo mật (optional)
3. Xử lý callback
Sau khi người dùng authorize, họ sẽ được redirect về redirect_uri của bạn với code parameter:
https://yourapp.com/callback?code=AUTH_CODE&state=RANDOM_STRING4. Đổi code lấy access token
Gửi POST request đến endpoint token:
JavaScript
const response = await fetch('http://localhost:3000/api/oauth/token', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
grant_type: 'authorization_code',
code: 'AUTH_CODE_FROM_CALLBACK',
client_id: 'YOUR_CLIENT_ID',
client_secret: 'YOUR_CLIENT_SECRET',
redirect_uri: 'YOUR_CALLBACK_URL'
})
});
const data = await response.json();
const accessToken = data.access_token;
// {
// "access_token": "...",
// "token_type": "Bearer",
// "expires_in": 3600,
// "scope": "profile email"
// }Python
import requests
response = requests.post('http://localhost:3000/api/oauth/token', json={
'grant_type': 'authorization_code',
'code': 'AUTH_CODE_FROM_CALLBACK',
'client_id': 'YOUR_CLIENT_ID',
'client_secret': 'YOUR_CLIENT_SECRET',
'redirect_uri': 'YOUR_CALLBACK_URL'
})
data = response.json()
access_token = data['access_token']cURL
curl -X POST http://localhost:3000/api/oauth/token \
-H "Content-Type: application/json" \
-d '{
"grant_type": "authorization_code",
"code": "AUTH_CODE_FROM_CALLBACK",
"client_id": "YOUR_CLIENT_ID",
"client_secret": "YOUR_CLIENT_SECRET",
"redirect_uri": "YOUR_CALLBACK_URL"
}'5. Lấy thông tin user
Sử dụng access token để gọi API:
const userInfo = await fetch('http://localhost:3000/api/oauth/userinfo', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
}).then(r => r.json());
console.log(userInfo);
// {
// "id": "...",
// "email": "user@example.com",
// "full_name": "John Doe",
// "avatar_url": "https://..."
// }6. Lưu/đọc settings (Optional)
Nếu có scope settings:write, bạn có thể lưu settings:
// Lưu setting
await fetch('http://localhost:3000/api/settings', {
method: 'POST',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
client_id: 'YOUR_CLIENT_ID',
key: 'theme',
value: 'dark'
})
});
// Đọc settings
const settings = await fetch('http://localhost:3000/api/settings?client_id=YOUR_CLIENT_ID', {
headers: {
'Authorization': `Bearer ${accessToken}`
}
}).then(r => r.json());
console.log(settings.settings);💡 Lưu ý bảo mật
- Không bao giờ public Client Secret lên client-side code
- Exchange token phải được thực hiện từ server của bạn
- Luôn validate
stateparameter để chống CSRF - Access token expires sau 1 giờ
Hoàn tất!
Bạn đã tích hợp OAuth thành công. Xem thêm API Reference để biết chi tiết.