@app.get("/protected", response_class=HTMLResponse)
async def protected_route(request: Request, token: str = Query(...), authorized: bool = Depends(verify_token)):
# This route is protected, and the client must provide a valid access token obtained through OAuth2
# You can implement further logic here to handle the authenticated user
if authorized:
response = templates.TemplateResponse("protected.html", {"request": request})
response.set_cookie(key="access_token", value=token)
return response
return RedirectResponse(url="/")
Моя проблема в том, что когда я перенаправляю его обратно в шаблон, в строке URL отображается токен доступа. Я установил его как файл cookie и предпочел бы, чтобы он был там или в заголовке авторизации, к сожалению, это единственный способ получить его для проверки авторизации.

Как затем перенаправить пользователя на страницу без отображения токена доступа в строке URL?
PS это обратный вызов, который перенаправляет меня на защищенную конечную точку
@app.get("/auth/callback")
async def callback(code: str, state: str, request: Request):
# validate the response.
token_url, headers, body = client.prepare_token_request(
token_url=os.getenv("GOOGLE_TOKEN_URL"),
authorization_response=str(request.url),
redirect_url=os.getenv("REDIRECT_URI"),
code=code,
state=state
)
token_response = requests.post(
token_url,
headers=headers,
data=body,
auth=(os.getenv("CLIENT_ID"), os.getenv("CLIENT_SECRET")),
)
client.parse_request_body_response(token_response.text)
access_token = client.token.get("access_token")
refresh_token = client.token.get("refresh_token")
id_token = client.token.get("id_token")
# Now you can use the access_token to make requests to Google APIs or authenticate users
user_creds = {"access_token": access_token, "refresh_token": refresh_token, "id_token": id_token}
if access_token is not None:
# Redirect back to the protected page (replace with your actual protected page URL)
return RedirectResponse(url=f"/protected?token={access_token}")
else:
raise HTTPException(status_code=401, detail="Authentication failed")
Подробнее здесь: https://stackoverflow.com/questions/781 ... h-fast-api