ใช้ Endpoint นี้เพื่อแลก client_id และ client_secret เป็น Access Token สำหรับเรียกใช้งาน Partner API
Partner API ใช้รูปแบบ OAuth 2.0 Client Credentials Flow โดย Application จะยืนยันตัวตนด้วย Client Credentials ของตนเอง และได้รับ Bearer Token สำหรับใช้ในการเรียก API
Access Token สามารถนำกลับมาใช้ซ้ำได้จนกว่าจะหมดอายุ
ไม่ควรเรียก /oauth/token ทุกครั้งก่อนเรียก API เนื่องจากจะเพิ่มภาระให้ระบบโดยไม่จำเป็น
แนวทางที่แนะนำ:
expires_in)401 invalid_tokenPOST /api/partner/v1/oauth/token
Content-Type: application/x-www-form-urlencoded
Authorization Header ไม่จำเป็นสำหรับ Endpoint นี้
| Field | Type | Required | Description |
|---|---|---|---|
| grant_type | string | Yes | ต้องเป็น client_credentials |
| client_id | string | Yes | Client ID ที่ได้รับจาก Open API Settings |
| client_secret | string | Yes | Client Secret ที่ได้รับตอนสร้าง Application |
curl -X POST https://<host>/api/partner/v1/oauth/token \
-H "Content-Type: application/x-www-form-urlencoded" \
-d "grant_type=client_credentials" \
-d "client_id=meed_0cb08343fffe4134028921bf07882ea8" \
-d "client_secret=b47f8cb732a93dd355565a185a6a04efc03ca62fd707fbf51261744228f13d76"
{
"access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"token_type": "Bearer",
"expires_in": 3600
}
| Field | Type | Description |
|---|---|---|
| access_token | string | JWT Access Token สำหรับเรียก Partner API |
| token_type | string | ค่าเป็น Bearer เสมอ |
| expires_in | number | อายุของ Token (วินาที) |
Access Token มีอายุจำกัดตามค่า expires_in
เมื่อ Token หมดอายุ:
/oauth/tokenระบบไม่รองรับ Refresh Token
ดังนั้นเมื่อ Access Token หมดอายุ ให้เรียก Endpoint นี้อีกครั้งด้วย client_id และ client_secret เดิม
แนบ Access Token ใน Authorization Header ของทุก Partner API Request
Authorization: Bearer <access_token>
ตัวอย่าง:
curl https://<host>/api/partner/v1/sales \
-H "Authorization: Bearer <access_token>"
client_secret เปรียบเสมือนรหัสผ่านของ Application
ควรเก็บไว้เฉพาะฝั่ง Server เท่านั้น
ไม่ควร:
หากสงสัยว่า Client Secret รั่วไหล ควรสร้าง Application ใหม่และยกเลิกการใช้งาน Credentials เดิมทันที
Partner API รองรับเฉพาะ HTTPS เพื่อป้องกันการดักจับข้อมูลระหว่างทาง
400 Bad Request
{
"error": "invalid_request",
"message": "grant_type, client_id, and client_secret are required and grant_type must be \"client_credentials\""
}
สาเหตุ:
client_credentials401 Unauthorized
{
"error": "invalid_client",
"message": "Invalid client_id or client_secret"
}
สาเหตุ:
401 Unauthorized
{
"error": "invalid_client",
"message": "API application is suspended or revoked"
}
สาเหตุ: