【发布时间】:2022-01-30 03:39:39
【问题描述】:
我正在测试使用 FastAPI 制作 OAuth 服务器。我有一侧,即 Oauth 服务器,它的端点如下所示:
@router.post("/token")
async def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()) -> Dict[str, str]:
return {"access_token": form_data.username, "token_type": "bearer"}
然后在一个单独的进程中,我有一个带有登录端点的假应用程序,如下所示:
@router.post("/")
async def login(username: str, password: str) -> Dict[str, str]:
form = OAuth2PasswordRequestForm(
username=username,
password=password,
scope="me"
)
form_data = aiohttp.FormData()
for key, val in form.__dict__.items():
form_data.add_field(key, val)
async with aiohttp.ClientSession() as session:
async with session.post(f"http://localhost:8001/oauth/token", data=form_data()) as server_response:
response = await server_response.text()
response = json.loads(response)
return response
OAuth 服务器正在响应
{
"detail": [
{
"loc": [
"body",
"grant_type"
],
"msg": "string does not match regex \"password\"",
"type": "value_error.str.regex",
"ctx": {
"pattern": "password"
}
}
]
}
如何使用 aiohttp 将表单数据发布到 oauth 服务器?
【问题讨论】:
-
它抱怨表单数据不包括
grant_type键值password;您发送的数据中似乎没有包含一个?
标签: python oauth-2.0 fastapi aiohttp