【发布时间】:2022-02-04 08:27:33
【问题描述】:
我正在尝试将请求从 protected_api() 重定向到 fastapi 中的 login() 函数。但它失败了消息
获取失败。 可能的原因: CORS 网络故障 对于 CORS 请求,URL 方案必须是“http”或“https”。
可能是什么问题以及如何从一个 api 重定向到另一个 api
@app.get("/protected_api")
async def protected_api():
resp = RedirectResponse("https://localhost:5000/token")
return resp
@app.post("/token", response_model=Token)
async def login(form_data: OAuth2PasswordRequestForm = Depends()): # login function to get access token
print('In login fun value of form_data dict.....%s' % form_data.__dict__)
user = authenticate_user(fake_users_db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(seconds=ACCESS_TOKEN_EXPIRE_SECONDS)
access_token = create_access_token(
data={"sub": user.username}, expires_delta=access_token_expires
)
print('Value of access_token in login fun ......%s\n' % access_token)
return {"access_token": access_token, "token_type": "bearer"}
【问题讨论】:
-
您需要提供更多信息 - 因为它没有
http或https作为协议,所以错误输出的请求到底是什么样的?
标签: fastapi