使用 Python 3.9,我尝试遵循 FastAPI 安全教程,但我无法设法显示 authorize 按钮,也无法显示路径附近的 lock。
在我的代码中(出于安全目的,我无法完全显示)我做了文档中出现的所有操作:
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="users/login")
@app.post("/users/login")
async def logIn(form_data: OAuth2PasswordRequestForm = Depends()):
token = management.login(form_data.username, form_data.password)
if token == False:
raise HTTPException(status_code=400, detail="Incorrect username or password")
else:
return token
#Fake path for privacy reasons
@app.get("/fake/path")
async def read_item(token: Annotated[str, Depends(oauth2_scheme)]):
return True
然而Swagger并不是这样出现的
但看起来像
为什么会出现这种情况?
PS:我也尝试过这样
async def logIn(form_data: Annotated[OAuth2PasswordRequestForm, Depends()])
但我得到了fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that typing.Annotated[fastapi.security.oauth2.OAuth2PasswordRequestForm, Depends(NoneType)] is a valid pydantic field type
OAuth2PasswordRequestForm = Depends()
取决于什么?你需要通过一个验证函数