farmentano12 commited on
Commit
4a246f7
verified
1 Parent(s): a6ab760

Fixing Google authentication

Browse files
Files changed (1) hide show
  1. app.py +28 -13
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
- # --- Si us谩s secret GCP_SA_JSON en Hugging Face, pod茅s cargarlo as铆:
105
- # import json, pathlib
106
- # sa_json = os.getenv("GCP_SA_JSON")
107
- # if sa_json:
108
- # from google.oauth2 import service_account
109
- # info = json.loads(sa_json)
110
- # creds = service_account.Credentials.from_service_account_info(info)
111
- # project = info.get("project_id") or BQ_PROJECT
112
- # return bigquery.Client(project=project, credentials=creds)
113
- # ---
114
- if not os.getenv("GOOGLE_APPLICATION_CREDENTIALS"):
115
- raise RuntimeError("GOOGLE_APPLICATION_CREDENTIALS no seteado.")
116
- return bigquery.Client(project=BQ_PROJECT)
 
 
 
 
 
 
 
 
 
 
 
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()