timmy / 使用 FastAPI 進行 API 身份驗證與請求處理
0 喜欢
0 派生
3 文件
最后活跃于 9 months ago
| 1 | from fastapi import FastAPI, Header, HTTPException |
| 2 | |
| 3 | app = FastAPI() |
| 4 | |
| 5 | @app.get("/secure-data") |
| 6 | async def secure_data(api_key: str = Header(None)): |
| 7 | if api_key != "my_secure_token": |
| 8 | raise HTTPException(status_code=401, detail="無效的 API 金鑰") |
| 9 | return {"message": "驗證成功,提供安全數據"} |
上一页
下一页