วิธีเชื่อมต่อแบบ Client Credentials
การขอ Access token
- สร้าง POST request ไปยัง https://openapi.flowaccount.com/test/token โดยกำหนดดังนี้
- เมื่อสำเร็จจะได้ response สามารถใช้
access_token
ไปใช้ส่ง request กับ OpenAPI ซึ่ง token เป็นแบบ Bearer token - ในการส่ง request ให้ระบุ token ที่ได้ ใน header
authorization
เช่น ได้ access_tokenDZ61byhdxZVHvTgE8VN4ixtIeSJqNO7TPnF8TtjSGig
ให้ตั้งค่า authorization เป็นBearer DZ61byhdxZVHvTgE8VN4ixtIeSJqNO7TPnF8TtjSGig
info
- โดยทั่วไป access token แบบ client credentials จะมีอายุ 1 วัน (86,400 วินาที อ้างอิงจากค่าของ
expires_in
ใน response) - การขอ access token ทุกครั้งจะได้ token ใหม่ ในขณะที่ token ก่อนหน้าจะยังคงใช้งานได้อยู่จนกว่า token นั้น ๆ จะหมดอายุ
- แนะนำให้ client จัดเก็บ token ที่ได้ไว้ในระบบเพื่อใช้ซ้ำจนกว่าจะหมดอายุแล้วจึงส่ง request ขอใหม่อีกคั้ง
API Specifications
อ้างอิง API Reference
Request Header
Field | Value |
---|---|
content-type | application/x-www-form-urlencoded |
Request Body
Field | Value |
---|---|
grant_type | client_credentials |
scope | flowaccount-api |
client_id | client id ที่ได้รับ |
client_secret | client secret ที่ได้รับ |
curl example
curl --location 'https://openapi.flowaccount.com/test/token' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'grant_type=refresh_token' \
--data-urlencode 'client_id=sample-sandbox-client' \
--data-urlencode 'client_secret=bVb5da6kvKMKX9b066UZ0dXnN689E-emvm_nYFXR4eg' \
--data-urlencode 'scope=flowaccount-api'
Response body
Field | type | Description |
---|---|---|
access_token | string | access token สำหรับเข้าถึง resource ภายใต้ FlowAccount OpenAPI |
expires_in | number | อายุของ access token หน่วยเป็นวินาที |
token_type | string | ประเภทของ access token ปกติเป็น bearer token |
scope | string | resource scope ของ access token |
Response example
{
"access_token": "DZ61byhdxZVHvTgE8VN4ixtIeSJqNO7TPnF8TtjSGig",
"expires_in": 86400,
"token_type": "Bearer",
"scope": "flowaccount-api"
}
Errors
หากการขอ token เกิดข้อผิดพลาดจากข้อมูล request ที่ไม่ครบถ้วน
โดยทั่วไปจะได้รับ HTTP Response 200 และ body ที่มี field error
เป็นข้อความระบุปัญหาหรือสาเหตุเบื้องต้น เช่น
{
"error": "unsupported_grant_type"
}
ทั้งนี้ข้อความและสาเหตุที่เป็นไปได้ มีดังนี้
Error message | Possible causes |
---|---|
unsupported_grant_type | ค่าของ grant_type ไม่ถูกต้องตามที่กำหนด |
invalid_client | ไม่พบ client_id หรือ client_secret หรือค่าไม่ถูกต้อง |
invalid_scope | ค่าของ scope ไม่ถูกต้องตามที่กำหนด |