OAuth 2.0
OAuth 2.0 cung cấp cơ chế xác thực an toàn dựa trên token cho các ứng dụng bên thứ ba truy cập Brevo thay mặt người dùng.
Tổng quan luồng OAuth
- Yêu cầu ủy quyền: Chuyển hướng người dùng sang Brevo
- Người dùng ủy quyền: Người dùng cấp quyền truy cập
- Mã ủy quyền: Brevo chuyển hướng trở lại kèm theo mã
- Đổi lấy access token: Đổi mã ủy quyền lấy token
- Truy cập API: Dùng access token cho các yêu cầu
Endpoint ủy quyền
https://app.brevo.com/oauth/authorize? response_type=code& client_id=YOUR_CLIENT_ID& redirect_uri=YOUR_REDIRECT_URI& scope=email%20contacts& state=random_stringĐổi lấy token
curl -X POST "https://api.brevo.com/v3/oauth/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=authorization_code&code=AUTH_CODE&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET&redirect_uri=YOUR_REDIRECT_URI"Cách dùng access token
const response = await fetch('https://api.brevo.com/v3/account', { headers: { 'Authorization': 'Bearer YOUR_ACCESS_TOKEN', 'Accept': 'application/json' }});Các scope
email: Gửi email giao dịchcontacts: Quản lý liên hệ và danh sáchcampaigns: Tạo và gửi chiến dịchsms: Gửi tin nhắn SMSwebhooks: Quản lý webhook
Làm mới token
const refreshToken = async () => { const response = await fetch('https://api.brevo.com/v3/oauth/token', { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, body: new URLSearchParams({ grant_type: 'refresh_token', refresh_token: 'YOUR_REFRESH_TOKEN', client_id: 'YOUR_CLIENT_ID', client_secret: 'YOUR_CLIENT_SECRET' }) }); return response.json();};