Spaces:
Sleeping
Sleeping
Fixing Google authentication
Browse files
app.py
CHANGED
|
@@ -99,21 +99,36 @@ BQ_PROJECT = "leadgenios-tech"
|
|
| 99 |
BQ_TABLE_FQN = "leadgenios-tech.connector_appsflyer_raw_data.appsflyer_raw_data_daily_report"
|
| 100 |
|
| 101 |
def _need_bq_client():
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
if not _HAS_BQ:
|
| 103 |
raise RuntimeError("Falta dependencia 'google-cloud-bigquery'.")
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
def bq_get_columns_fixed():
|
| 119 |
client = _need_bq_client()
|
|
|
|
| 99 |
BQ_TABLE_FQN = "leadgenios-tech.connector_appsflyer_raw_data.appsflyer_raw_data_daily_report"
|
| 100 |
|
| 101 |
def _need_bq_client():
|
| 102 |
+
"""
|
| 103 |
+
Producci贸n (Hugging Face): usa el secret GCP_SA_JSON (contenido del JSON de la service account).
|
| 104 |
+
Local: si no hay GCP_SA_JSON, usa GOOGLE_APPLICATION_CREDENTIALS como fallback.
|
| 105 |
+
"""
|
| 106 |
if not _HAS_BQ:
|
| 107 |
raise RuntimeError("Falta dependencia 'google-cloud-bigquery'.")
|
| 108 |
+
|
| 109 |
+
sa_json = os.getenv("GCP_SA_JSON")
|
| 110 |
+
if sa_json:
|
| 111 |
+
import json
|
| 112 |
+
try:
|
| 113 |
+
from google.oauth2 import service_account
|
| 114 |
+
except Exception as e:
|
| 115 |
+
raise RuntimeError(f"No se pudo importar google.oauth2.service_account: {e}")
|
| 116 |
+
try:
|
| 117 |
+
info = json.loads(sa_json)
|
| 118 |
+
creds = service_account.Credentials.from_service_account_info(info)
|
| 119 |
+
project = info.get("project_id") or BQ_PROJECT
|
| 120 |
+
return bigquery.Client(project=project, credentials=creds)
|
| 121 |
+
except Exception as e:
|
| 122 |
+
raise RuntimeError(f"GCP_SA_JSON inv谩lido o no utilizable: {e}")
|
| 123 |
+
|
| 124 |
+
# Fallback local
|
| 125 |
+
if os.getenv("GOOGLE_APPLICATION_CREDENTIALS"):
|
| 126 |
+
try:
|
| 127 |
+
return bigquery.Client(project=BQ_PROJECT)
|
| 128 |
+
except Exception as e:
|
| 129 |
+
raise RuntimeError(f"Error creando cliente BQ con GOOGLE_APPLICATION_CREDENTIALS: {e}")
|
| 130 |
+
|
| 131 |
+
raise RuntimeError("No hay credenciales: sete谩 GCP_SA_JSON (prod) o GOOGLE_APPLICATION_CREDENTIALS (local).")
|
| 132 |
|
| 133 |
def bq_get_columns_fixed():
|
| 134 |
client = _need_bq_client()
|