Skip to main content

วิธีเชื่อมต่อแบบ Client Credentials

การขอ Access token

  1. สร้าง POST request ไปยัง https://openapi.flowaccount.com/test/token โดยกำหนดดังนี้
  2. เมื่อสำเร็จจะได้ response สามารถใช้ access_token ไปใช้ส่ง request กับ OpenAPI ซึ่ง token เป็นแบบ Bearer token
  3. ในการส่ง request ให้ระบุ token ที่ได้ ใน header authorization เช่น ได้ access_token DZ61byhdxZVHvTgE8VN4ixtIeSJqNO7TPnF8TtjSGig ให้ตั้งค่า 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

FieldValue
content-typeapplication/x-www-form-urlencoded

Request Body

FieldValue
grant_typeclient_credentials
scopeflowaccount-api
client_idclient id ที่ได้รับ
client_secretclient 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

FieldtypeDescription
access_tokenstringaccess token สำหรับเข้าถึง resource ภายใต้ FlowAccount OpenAPI
expires_innumberอายุของ access token หน่วยเป็นวินาที
token_typestringประเภทของ access token ปกติเป็น bearer token
scopestringresource 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 messagePossible causes
unsupported_grant_typeค่าของ grant_type ไม่ถูกต้องตามที่กำหนด
invalid_clientไม่พบ client_id หรือ client_secret หรือค่าไม่ถูกต้อง
invalid_scopeค่าของ scope ไม่ถูกต้องตามที่กำหนด