FastAPI 安全 OAuthPassword2Bearer 和令牌管理无法正常工作

问题描述 投票:0回答:1

使用 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并不是这样出现的

文档示例:https://i.stack.imgur.com/awxQU.png

但看起来像

我的招摇:https://i.stack.imgur.com/Y66Rd.png

为什么会出现这种情况?

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

python security oauth-2.0 jwt fastapi
1个回答
0
投票
OAuth2PasswordRequestForm = Depends()

取决于什么?你需要通过一个验证函数

© www.soinside.com 2019 - 2024. All rights reserved.