Script agregar_planos.py ajustado para cuenta de servicio.
This commit is contained in:
@@ -24,9 +24,6 @@ for k in list(sys.modules.keys()):
|
|||||||
from googleapiclient.discovery import build
|
from googleapiclient.discovery import build
|
||||||
from googleapiclient.errors import HttpError
|
from googleapiclient.errors import HttpError
|
||||||
from google.oauth2 import service_account
|
from google.oauth2 import service_account
|
||||||
from google.oauth2.credentials import Credentials
|
|
||||||
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
||||||
from google.auth.transport.requests import Request as GRequest
|
|
||||||
|
|
||||||
# --------------------
|
# --------------------
|
||||||
# Rocketbot vars helpers
|
# Rocketbot vars helpers
|
||||||
@@ -68,40 +65,23 @@ def _load_json(path):
|
|||||||
with open(path, "r", encoding="utf-8") as f:
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
def get_services(credentials_json_path, token_json_path):
|
def get_services(credentials_json_path, impersonated_user):
|
||||||
info = _load_json(credentials_json_path)
|
info = _load_json(credentials_json_path)
|
||||||
|
if not isinstance(info, dict) or info.get("type") != "service_account":
|
||||||
|
raise RuntimeError("gdoc_sa_json2 debe apuntar a un JSON de cuenta de servicio.")
|
||||||
|
|
||||||
# Service Account
|
impersonated_user = (impersonated_user or "").strip()
|
||||||
if isinstance(info, dict) and info.get("type") == "service_account":
|
if not impersonated_user:
|
||||||
creds = service_account.Credentials.from_service_account_file(
|
raise RuntimeError("Falta la variable gdoc_impersonated_user.")
|
||||||
credentials_json_path, scopes=SCOPES
|
|
||||||
)
|
|
||||||
docs = build("docs", "v1", credentials=creds, cache_discovery=False)
|
|
||||||
drive = build("drive", "v3", credentials=creds, cache_discovery=False)
|
|
||||||
return docs, drive, "service_account"
|
|
||||||
|
|
||||||
# OAuth Desktop
|
|
||||||
creds = None
|
|
||||||
if os.path.exists(token_json_path):
|
|
||||||
creds = Credentials.from_authorized_user_file(token_json_path, SCOPES)
|
|
||||||
|
|
||||||
if (not creds) or (not creds.valid):
|
|
||||||
if creds and creds.expired and creds.refresh_token:
|
|
||||||
creds.refresh(GRequest())
|
|
||||||
else:
|
|
||||||
flow = InstalledAppFlow.from_client_secrets_file(credentials_json_path, SCOPES)
|
|
||||||
try:
|
|
||||||
creds = flow.run_local_server(port=0)
|
|
||||||
except Exception:
|
|
||||||
creds = flow.run_console()
|
|
||||||
|
|
||||||
os.makedirs(os.path.dirname(token_json_path), exist_ok=True)
|
|
||||||
with open(token_json_path, "w", encoding="utf-8") as f:
|
|
||||||
f.write(creds.to_json())
|
|
||||||
|
|
||||||
|
creds = service_account.Credentials.from_service_account_file(
|
||||||
|
credentials_json_path,
|
||||||
|
scopes=SCOPES,
|
||||||
|
subject=impersonated_user,
|
||||||
|
)
|
||||||
docs = build("docs", "v1", credentials=creds, cache_discovery=False)
|
docs = build("docs", "v1", credentials=creds, cache_discovery=False)
|
||||||
drive = build("drive", "v3", credentials=creds, cache_discovery=False)
|
drive = build("drive", "v3", credentials=creds, cache_discovery=False)
|
||||||
return docs, drive, "oauth"
|
return docs, drive, "service_account_impersonated"
|
||||||
|
|
||||||
# --------------------
|
# --------------------
|
||||||
# Drive helpers
|
# Drive helpers
|
||||||
@@ -568,24 +548,20 @@ try:
|
|||||||
url1 = _gvs("url_plano_producto", "")
|
url1 = _gvs("url_plano_producto", "")
|
||||||
url2 = _gvs("url_plano_piso", "")
|
url2 = _gvs("url_plano_piso", "")
|
||||||
|
|
||||||
cred_path = _gvs("gdoc_sa_json", "")
|
cred_path = _gvs("gdoc_sa_json2", "")
|
||||||
if cred_path == "":
|
if cred_path == "":
|
||||||
raise RuntimeError("Falta gdoc_sa_json (ruta a credentials.json)")
|
raise RuntimeError("Falta gdoc_sa_json2 (ruta al JSON de cuenta de servicio)")
|
||||||
if not os.path.isabs(cred_path):
|
if not os.path.isabs(cred_path):
|
||||||
cred_path = os.path.join(base_dir, cred_path)
|
cred_path = os.path.join(base_dir, cred_path)
|
||||||
if not os.path.exists(cred_path):
|
if not os.path.exists(cred_path):
|
||||||
raise RuntimeError("No existe credentials.json: " + cred_path)
|
raise RuntimeError("No existe JSON de cuenta de servicio: " + cred_path)
|
||||||
|
|
||||||
token_path = _gvs("gdoc_token_json", "")
|
impersonated_user = _gvs("gdoc_impersonated_user", "")
|
||||||
if token_path == "":
|
|
||||||
token_path = os.path.join(base_dir, "credentials", "token_gdocs_drive.json")
|
|
||||||
if not os.path.isabs(token_path):
|
|
||||||
token_path = os.path.join(base_dir, token_path)
|
|
||||||
|
|
||||||
_sv("gdoc_credentials_used", cred_path)
|
_sv("gdoc_credentials_used", cred_path)
|
||||||
_sv("gdoc_token_used", token_path)
|
_sv("gdoc_impersonated_user_used", impersonated_user)
|
||||||
|
|
||||||
docs_service, drive_service, auth_mode = get_services(cred_path, token_path)
|
docs_service, drive_service, auth_mode = get_services(cred_path, impersonated_user)
|
||||||
_sv("gdoc_auth_mode", auth_mode)
|
_sv("gdoc_auth_mode", auth_mode)
|
||||||
|
|
||||||
# doc_id desde URL del navegador
|
# doc_id desde URL del navegador
|
||||||
|
|||||||
Reference in New Issue
Block a user