Tutti Nuovi

user:thomas utente che ha creato il gist

title:mygist titolo del gist

filename:myfile.txt nome di file nel gist

extension:yml estensione del file nel gist

language:go linguaggio del file nel gist

topic:homelab gists with given topic

Entra


Tutti Nuovi Entra

All gists matching topic fastapi

Più recente creazione
Meno recente creazione
Più recente aggiornamento
Meno recente aggiornamento

timmy / 使用 FastAPI 進行 API 身份驗證與請求處理

0 mi piace
0 forks
3 files
Ultima volta attivo 9 months ago
FastAPI 可透過 Header 讀取請求標頭,並透過 Depends 進行依賴注入(Dependency Injection),適用於 API Token 驗證、身份驗證、請求處理 等場景。
api authentication fastapi header python security
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": "驗證成功,提供安全數據"}
Più nuovi Più vecchi

Creato da Opengist ⋅ Load: 18ms⋅

Italiano
Čeština Deutsch English Español Français Magyar Italiano 日本語 Polski Português Русский Türkçe Українська 中文 繁體中文