Spaces:
Sleeping
Sleeping
Update app/main.py
Browse files- app/main.py +11 -11
app/main.py
CHANGED
|
@@ -8,8 +8,7 @@ from fastapi import FastAPI, Query, Response
|
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
|
| 10 |
from pydantic import BaseModel
|
| 11 |
-
from app.google_oauth import build_flow, save_credentials
|
| 12 |
-
|
| 13 |
|
| 14 |
from .agent import ShoppingAgent
|
| 15 |
from .feedback import caminho_feedback, google_sheets_habilitado, salvar_feedback
|
|
@@ -119,28 +118,29 @@ def auth_google():
|
|
| 119 |
@app.get("/oauth2callback")
|
| 120 |
def oauth2callback(request: Request):
|
| 121 |
try:
|
| 122 |
-
state = request.query_params.get("state")
|
| 123 |
code = request.query_params.get("code")
|
| 124 |
|
| 125 |
if not code:
|
| 126 |
-
return HTMLResponse(
|
| 127 |
-
<h2>Erro OAuth</h2>
|
| 128 |
-
<p>Code não recebido</p>
|
| 129 |
-
<pre>{request.url}</pre>
|
| 130 |
-
""", status_code=400)
|
| 131 |
|
| 132 |
-
flow = build_flow(
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
flow.fetch_token(code=code)
|
| 135 |
-
|
| 136 |
save_credentials(flow.credentials)
|
| 137 |
|
| 138 |
return HTMLResponse("""
|
| 139 |
<h2>✅ Google Sheets autorizado com sucesso!</h2>
|
|
|
|
| 140 |
""")
|
| 141 |
|
| 142 |
except Exception as e:
|
| 143 |
-
return HTMLResponse(
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
@app.get("/auth/status")
|
|
|
|
| 8 |
from fastapi.middleware.cors import CORSMiddleware
|
| 9 |
from fastapi.responses import FileResponse, HTMLResponse, RedirectResponse
|
| 10 |
from pydantic import BaseModel
|
| 11 |
+
from app.google_oauth import build_flow, save_credentials, OAUTH_TEMP
|
|
|
|
| 12 |
|
| 13 |
from .agent import ShoppingAgent
|
| 14 |
from .feedback import caminho_feedback, google_sheets_habilitado, salvar_feedback
|
|
|
|
| 118 |
@app.get("/oauth2callback")
|
| 119 |
def oauth2callback(request: Request):
|
| 120 |
try:
|
|
|
|
| 121 |
code = request.query_params.get("code")
|
| 122 |
|
| 123 |
if not code:
|
| 124 |
+
return HTMLResponse("Erro: código OAuth não recebido.", status_code=400)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
flow = build_flow(
|
| 127 |
+
state=OAUTH_TEMP.get("state"),
|
| 128 |
+
code_verifier=OAUTH_TEMP.get("code_verifier"),
|
| 129 |
+
)
|
| 130 |
|
| 131 |
flow.fetch_token(code=code)
|
|
|
|
| 132 |
save_credentials(flow.credentials)
|
| 133 |
|
| 134 |
return HTMLResponse("""
|
| 135 |
<h2>✅ Google Sheets autorizado com sucesso!</h2>
|
| 136 |
+
<p>Agora o backend pode salvar feedbacks na planilha.</p>
|
| 137 |
""")
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
+
return HTMLResponse(
|
| 141 |
+
f"<h2>Erro no OAuth callback</h2><pre>{type(e).__name__}: {str(e)}</pre>",
|
| 142 |
+
status_code=500
|
| 143 |
+
)
|
| 144 |
|
| 145 |
|
| 146 |
@app.get("/auth/status")
|