Spaces:
Sleeping
Sleeping
Upload google_oauth.py
Browse files- google_oauth.py +144 -0
google_oauth.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
|
| 5 |
+
from google.auth.transport.requests import Request
|
| 6 |
+
from google.oauth2.credentials import Credentials
|
| 7 |
+
from google_auth_oauthlib.flow import Flow
|
| 8 |
+
from googleapiclient.discovery import build
|
| 9 |
+
|
| 10 |
+
SCOPES = [
|
| 11 |
+
"https://www.googleapis.com/auth/spreadsheets",
|
| 12 |
+
"https://www.googleapis.com/auth/drive.file",
|
| 13 |
+
]
|
| 14 |
+
TOKEN_FILENAME = "google_token.json"
|
| 15 |
+
DATA_DIR = Path("/data")
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class GoogleOAuthError(RuntimeError):
|
| 19 |
+
pass
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
class GoogleAuthRequiredError(GoogleOAuthError):
|
| 23 |
+
pass
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def get_token_path():
|
| 27 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 28 |
+
return DATA_DIR / TOKEN_FILENAME
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _client_config():
|
| 32 |
+
client_id = os.environ.get("GOOGLE_CLIENT_ID", "").strip()
|
| 33 |
+
client_secret = os.environ.get("GOOGLE_CLIENT_SECRET", "").strip()
|
| 34 |
+
redirect_uri = os.environ.get("GOOGLE_REDIRECT_URI", "").strip()
|
| 35 |
+
|
| 36 |
+
missing = [
|
| 37 |
+
name
|
| 38 |
+
for name, value in {
|
| 39 |
+
"GOOGLE_CLIENT_ID": client_id,
|
| 40 |
+
"GOOGLE_CLIENT_SECRET": client_secret,
|
| 41 |
+
"GOOGLE_REDIRECT_URI": redirect_uri,
|
| 42 |
+
}.items()
|
| 43 |
+
if not value
|
| 44 |
+
]
|
| 45 |
+
if missing:
|
| 46 |
+
raise GoogleOAuthError("Variaveis OAuth ausentes: " + ", ".join(missing))
|
| 47 |
+
|
| 48 |
+
return {
|
| 49 |
+
"web": {
|
| 50 |
+
"client_id": client_id,
|
| 51 |
+
"client_secret": client_secret,
|
| 52 |
+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
|
| 53 |
+
"token_uri": "https://oauth2.googleapis.com/token",
|
| 54 |
+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
|
| 55 |
+
"redirect_uris": [redirect_uri],
|
| 56 |
+
}
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def build_flow(state=None):
|
| 61 |
+
redirect_uri = os.environ.get("GOOGLE_REDIRECT_URI", "").strip()
|
| 62 |
+
flow = Flow.from_client_config(_client_config(), scopes=SCOPES, state=state)
|
| 63 |
+
flow.redirect_uri = redirect_uri
|
| 64 |
+
return flow
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def get_authorization_url():
|
| 68 |
+
flow = build_flow()
|
| 69 |
+
authorization_url, state = flow.authorization_url(
|
| 70 |
+
access_type="offline",
|
| 71 |
+
prompt="consent",
|
| 72 |
+
include_granted_scopes="true",
|
| 73 |
+
)
|
| 74 |
+
return authorization_url, state
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def save_credentials(credentials):
|
| 78 |
+
token_path = get_token_path()
|
| 79 |
+
token_path.write_text(credentials.to_json(), encoding="utf-8")
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
def load_credentials():
|
| 83 |
+
token_path = get_token_path()
|
| 84 |
+
if not token_path.exists():
|
| 85 |
+
return None
|
| 86 |
+
|
| 87 |
+
try:
|
| 88 |
+
payload = json.loads(token_path.read_text(encoding="utf-8"))
|
| 89 |
+
credentials = Credentials.from_authorized_user_info(payload, SCOPES)
|
| 90 |
+
except Exception:
|
| 91 |
+
return None
|
| 92 |
+
|
| 93 |
+
if credentials.expired and credentials.refresh_token:
|
| 94 |
+
try:
|
| 95 |
+
credentials.refresh(Request())
|
| 96 |
+
save_credentials(credentials)
|
| 97 |
+
except Exception:
|
| 98 |
+
return None
|
| 99 |
+
|
| 100 |
+
if not credentials.valid:
|
| 101 |
+
return None
|
| 102 |
+
|
| 103 |
+
return credentials
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def get_sheets_service():
|
| 107 |
+
credentials = load_credentials()
|
| 108 |
+
if credentials is None:
|
| 109 |
+
raise GoogleAuthRequiredError(
|
| 110 |
+
"Google Sheets nao autorizado. Acesse /auth/google primeiro."
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
return build("sheets", "v4", credentials=credentials, cache_discovery=False)
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def append_feedback_to_sheet(feedback_data):
|
| 117 |
+
spreadsheet_id = os.environ.get("GOOGLE_SPREADSHEET_ID", "").strip()
|
| 118 |
+
if not spreadsheet_id:
|
| 119 |
+
raise GoogleOAuthError("GOOGLE_SPREADSHEET_ID nao configurado.")
|
| 120 |
+
|
| 121 |
+
service = get_sheets_service()
|
| 122 |
+
fields = [
|
| 123 |
+
"timestamp",
|
| 124 |
+
"query",
|
| 125 |
+
"product_id",
|
| 126 |
+
"product_name",
|
| 127 |
+
"categoria_inferida",
|
| 128 |
+
"feedback",
|
| 129 |
+
"motivo",
|
| 130 |
+
"score_final",
|
| 131 |
+
"score_semantico",
|
| 132 |
+
"bonus_lexical",
|
| 133 |
+
"penalidade_feedback",
|
| 134 |
+
"user_message",
|
| 135 |
+
]
|
| 136 |
+
row = [[str(feedback_data.get(field, "") or "") for field in fields]]
|
| 137 |
+
|
| 138 |
+
return service.spreadsheets().values().append(
|
| 139 |
+
spreadsheetId=spreadsheet_id,
|
| 140 |
+
range="Feedback!A:Z",
|
| 141 |
+
valueInputOption="USER_ENTERED",
|
| 142 |
+
insertDataOption="INSERT_ROWS",
|
| 143 |
+
body={"values": row},
|
| 144 |
+
).execute()
|