ตัวอย่างด้านล่างแสดงการใช้งาน API ผ่าน Python ใน 4 รูปแบบ:
ทุกตัวอย่างเริ่มต้นด้วยการขอ refresh-token และ access-token จาก username และ password
Python (requests): ด้านล่างเป็นตัวอย่างสคริปต์ Python ธรรมดาที่ใช้ requests เพื่อขอ token
Django: ใน Django ท่านสามารถใช้ Views เพื่อจัดการการขอ token และแสดงผลลัพธ์ในรูปแบบ JSON ให้กับ client
FastAPI: ใช้เส้นทาง (route) ของ FastAPI ในการขอ token และส่งกลับในรูปแบบ JSON
Flask: ใช้ฟังก์ชัน route ของ Flask ในการขอ token จาก API และส่ง JSON กลับไปยังผู้ร้องขอ
import requests
username = "your_username"
password = "your_password"
def get_tokens(username, password):
url = "https://hcode.moph.go.th/api/token/"
data = {"username": username, "password": password}
r = requests.post(url, json=data)
return r.json()
tokens = get_tokens(username, password)
refresh_token = tokens["refresh"]
access_token = tokens["access"]
print("Refresh Token:", refresh_token)
print("Access Token:", access_token)
import requests
from django.http import JsonResponse
from django.views import View
class GetTokensView(View):
def post(self, request):
username = request.POST.get("username", "your_username")
password = request.POST.get("password", "your_password")
url = "https://hcode.moph.go.th/api/token/"
data = {"username": username, "password": password}
r = requests.post(url, json=data)
return JsonResponse(r.json())
from fastapi import FastAPI, Body
import requests
app = FastAPI()
@app.post("/get-tokens")
def get_tokens(username: str = Body(...), password: str = Body(...)):
url = "https://hcode.moph.go.th/api/token/"
data = {"username": username, "password": password}
r = requests.post(url, json=data)
return r.json()
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route("/get-tokens", methods=["POST"])
def get_tokens():
username = request.json.get("username", "your_username")
password = request.json.get("password", "your_password")
url = "https://hcode.moph.go.th/api/token/"
data = {"username": username, "password": password}
r = requests.post(url, json=data)
return jsonify(r.json())
รันโค้ดหรือเรียก endpoint ข้างต้น ท่านจะได้รับ token ในรูปแบบ JSON
Python (requests): ด้านล่างคือการใช้ refresh-token เพื่อขอ access-token ใหม่ หาก access-token หมดอายุ
Django: ใน Django ท่านสามารถสร้าง View เพื่อเรียก refresh endpoint และส่งคืน access-token ใหม่เช่นเดียวกับการขอ token
FastAPI: เพิ่มเส้นทาง refresh token ใน FastAPI แล้วเรียกใช้งานคล้ายกับตอนขอ token
Flask: เพิ่ม route ใน Flask เพื่อส่ง refresh-token ไปยัง API และรับ access-token ใหม่
import requests
refresh_token = "your_refresh_token_here"
def renew_access_token(refresh_token):
url = "https://hcode.moph.go.th/api/token/refresh/"
data = {"refresh": refresh_token}
r = requests.post(url, json=data)
return r.json().get("access")
new_access_token = renew_access_token(refresh_token)
print("New Access Token:", new_access_token)
import requests
from django.http import JsonResponse
from django.views import View
class RefreshTokenView(View):
def post(self, request):
refresh = request.POST.get("refresh", "your_refresh_token_here")
url = "https://hcode.moph.go.th/api/token/refresh/"
r = requests.post(url, json={"refresh": refresh})
return JsonResponse(r.json())
from fastapi import FastAPI, Body
import requests
app = FastAPI()
@app.post("/refresh-token")
def refresh_token_endpoint(refresh: str = Body(...)):
url = "https://hcode.moph.go.th/api/token/refresh/"
r = requests.post(url, json={"refresh": refresh})
return r.json()
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
@app.route("/refresh-token", methods=["POST"])
def refresh_token():
refresh = request.json.get("refresh", "your_refresh_token_here")
url = "https://hcode.moph.go.th/api/token/refresh/"
r = requests.post(url, json={"refresh": refresh})
return jsonify(r.json())