Ajuste en script que envia el correo electronico con el contrato generado.

This commit is contained in:
mabejoyb
2026-05-13 14:24:02 -03:00
parent d0c1f77ec3
commit 252085029a

View File

@@ -1,7 +1,4 @@
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from google.oauth2.credentials import Credentials
from google.oauth2 import service_account from google.oauth2 import service_account
from googleapiclient.discovery import build from googleapiclient.discovery import build
import os import os
@@ -25,8 +22,8 @@ if os.path.isdir(libs_dir) and libs_dir not in sys.path:
SCOPES = {scopes_api_google} SCOPES = {scopes_api_google}
CREDENTIALS_PATH = '{gdoc_sa_json}' CREDENTIALS_PATH = '{gdoc_sa_json2}'
TOKEN_PATH = '{gmail_token_json}' IMPERSONATED_USER = '{gdoc_impersonated_user}'
def _load_json(path): def _load_json(path):
@@ -34,39 +31,17 @@ def _load_json(path):
return json.load(f) return json.load(f)
def get_drive_and_gmail_services(credentials_json_path=CREDENTIALS_PATH, token_json_path=TOKEN_PATH): def get_drive_and_gmail_services(credentials_json_path=CREDENTIALS_PATH, impersonated_user=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.')
impersonated_user = (impersonated_user or '').strip()
if not impersonated_user:
raise RuntimeError('Falta la variable gdoc_impersonated_user.')
# Service account
if isinstance(info, dict) and info.get('type') == 'service_account':
creds = service_account.Credentials.from_service_account_file( creds = service_account.Credentials.from_service_account_file(
credentials_json_path, scopes=SCOPES) credentials_json_path, scopes=SCOPES, subject=impersonated_user)
drive_service = build(
'drive', 'v3', credentials=creds, cache_discovery=False)
gmail_service = build(
'gmail', 'v1', credentials=creds, cache_discovery=False)
return drive_service, gmail_service
# OAuth flow
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(Request())
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())
drive_service = build( drive_service = build(
'drive', 'v3', credentials=creds, cache_discovery=False) 'drive', 'v3', credentials=creds, cache_discovery=False)
gmail_service = build( gmail_service = build(
@@ -121,7 +96,7 @@ def send_email(gmail_service, to_emails, subject, body_text, attachment_bytes, a
drive_service, gmail_service = get_drive_and_gmail_services( drive_service, gmail_service = get_drive_and_gmail_services(
CREDENTIALS_PATH, TOKEN_PATH) CREDENTIALS_PATH, IMPERSONATED_USER)
# Id del documento de Google Drive a enviar por email # Id del documento de Google Drive a enviar por email
DOC_ID = '{gdoc_id}' DOC_ID = '{gdoc_id}'