วิธีเชื่อมต่อแบบ Authorization Code
การขอ authorization code
- สร้าง Login URL เพื่อ redirect ผู้ใช้งานจากระบบต้นทางไปยัง FlowAccount Auth Server ด้วย format ดังนี้
- Authorize endpoint:
https://{{auth-server-endpoint}}/connect/authorize
- Query Parameters:
response_type
=code
scope
=flowaccount-api offline_access
client_id
= client id ของระบบต้นทางredirect_uri
= URL ที่จะให้ FlowAccount redirect ผู้ใช้กลับไปยังระบบต้นทาง ซึ่งจะต้องเป็น uri ที่ได้กำหนดไว้ก่อนแล้วเท่านั้นstate
= ข้อความเข้ารหัสอ้างอิงสำหรับฝั่ง client ซึ่ง field นี้เป็น optional
- Example:
https://sandbox-auth.flowaccount.com/connect/authorize?response_type=code&scope=flowaccount-api%20offline_access&client_id=id&redirect_uri=https:\\localhost:3000
- Authorize endpoint:
- ผู้ใช้งานถูก redirect ไปยังหน้า login ของ FlowAccount ซึ่งจะต้อง login ด้วยผู้ใช้งานและรหัสของ FlowAccount เมื่อ login สำเร็จผู้ใช้จะต้องเลือกธุรกิจของ FlowAccount ที่จะอนุญาตให้ระบบต้นทางเชื่อมต่อ
- หลังจากผู้ใช้งานเลือกธุรกิจแล้ว FlowAccount จะ redirect ผู้ใช้งานกลับไปยัง redirect URI ที่ส่งมาในขั้นตอนที่ 1 พร้อมกับ Query parameters ต่อไปนี้
code
= Authorization code ที่จะนำไปใช้ขอ access token ต่อไปscope
= scope ที่ส่ง request มาในขั้นตอนที่ 1state
= ข้อความเข้ารหัสอ้างอิงเดียวกันกับที่ส่ง request มาในขั้นตอนที่ 1 หากระบบต้นทางได้ส่งค่านี้มา
info
- Query parameters ใน Login URL จะต้องถูกเข้ารหัสเป็น URL-encoded มาแล้ว เพื่อแปลงอักขระพิเศษ ให้สามารถส่งผ่าน HTTP request ได้
- Scope ควรจะมีค่า
offline_access
ด้วยเพื่อให้ระบบแนบ refresh token มาในเวลาขอ access token หากไม่ได้ใส่ scope นี้ response จะมีเพียงแค่ access token เพียงอย่างเดียว
การขอ access token
- สร้าง POST request แบบ x-www-form-urlencoded ไปยัง https://openapi.flowaccount.com/{{environment}}/token โดยกำหนดค่าดังนี้
grant_type
=authorization_code
code
= Authorization code ที่ได้มาจาก callback URL หลังจากผู้ใช้งาน login เสร็จredirect_uri
= Redirect URI ของระบบต้นทางที่ได้รับ callback มาclient_id
= client id ของระบบต้นทางclient_secret
= client secret ของระบบต้นทาง
- เมื่อสำเร็จ จะได้ response ที่มีข้อมูลดังต่อไปนี้
access_token
= Access token สำหรับธุรกิจที่ผู้ใช้งานได้เลือกไว้ในขั้นตอนการขอ authorization codeexpires_in
= อายุของ access token มีหน ่วยเป็นวินาทีtoken_type
= ประเภทของ access token ปกติเป็น bearer tokensupportCode
= รหัสลูกค้าของธุรกิจที่ผู้ใช้งานเลือกไว้ สามารถนำมาใช้เป็นรหัสอ้้างอิงถึงธุรกิจบน FlowAccount จากระบบภายนอกได้refresh_token
= refresh token สำหรับใช้ขอ refresh access token ใหม่ภายใต้ authorization ปัจจุบันของผู้ใช้งาน
info
- Refresh token โดยทั่วไปจะมีอายุ 1 วันนับจากการขอ access token ครั้งล่าสุด
- Authorization code จะมีอายุ 150 นาทีนับจากที่ผู้ใช้งานเลือกธุรกิจสำเร็จและใช้งานได้เพียงครั้งเดียว
- หาก authorization code หรือ refresh token หมดอายุ จะไม่สามารถขอ access token ได้อีก และจำเป็นจะต้องให้ผู้ใช้งานดำเนินการใหม่ ตั้งแต่การขอ authorization code
การขอ refresh access token
- สร้าง POST request แบบ x-www-form-urlencoded ไปยัง https://openapi.flowaccount.com/{{environment}}/token โดยกำหนดค่าดังนี้
grant_type
=refresh_token
refresh_token
= refresh token ที่ได้จากการขอ access token หรือขอ refresh token ครั้งล่าสุดclient_id
client_secret
- เมื่อสำเร็จจะได้ response เช่นเดียวกับการขอ access token
warning
Refresh token จะถูกสร้างใหม่ทุกครั้งที่ขอ refresh access token ดังนั้นฝั่ง client ต้องอัพเดต refresh token ที่มีอยู่ตาม response ล่าสุดเท่านั้น