import json import sys from google.oauth2 import service_account from googleapiclient.discovery import build import google.auth.exceptions KEY_FILE = '/home/hermeswebui/.hermes/webui/attachments/00e47f151810/ga4searchcomsol-e5e7219f50e9.json' SCOPES = [ 'https://www.googleapis.com/auth/webmasters.readonly', 'https://www.googleapis.com/auth/analytics.readonly', 'https://www.googleapis.com/auth/tagmanager.readonly' ] def check_gsc(creds): print("--- 1. Search Console ---") try: service = build('searchconsole', 'v1', credentials=creds) sites = service.sites().list().execute() site_list = sites.get('siteEntry', []) if site_list: print(f"SUCCESS: Connected! Found {len(site_list)} site(s):") for s in site_list: print(f" - {s.get('siteUrl')} (Permission: {s.get('permissionLevel')})") else: print("AUTH OK, but 0 sites found (Service Account not added as user in Search Console properties yet).") except Exception as e: print("ERROR:", e) def check_gtm(creds): print("\n--- 2. Google Tag Manager ---") try: service = build('tagmanager', 'v2', credentials=creds) accounts = service.accounts().list().execute() acc_list = accounts.get('account', []) if acc_list: print(f"SUCCESS: Connected! Found {len(acc_list)} account(s):") for a in acc_list: print(f" - Account: {a.get('name')} (ID: {a.get('accountId')})") else: print("AUTH OK, but 0 accounts found (Service Account not added to GTM Account/Container).") except Exception as e: print("ERROR:", e) def check_ga4(creds): print("\n--- 3. Google Analytics (Admin API) ---") try: service = build('analyticsadmin', 'v1beta', credentials=creds) accounts = service.accounts().list().execute() acc_list = accounts.get('accounts', []) if acc_list: print(f"SUCCESS: Connected! Found {len(acc_list)} GA account(s):") for a in acc_list: print(f" - {a.get('displayName')} ({a.get('name')})") else: print("AUTH OK, but 0 accounts found (Service Account not added to GA4 Property/Account).") except Exception as e: print("ERROR:", e) if __name__ == '__main__': try: creds = service_account.Credentials.from_service_account_file(KEY_FILE, scopes=SCOPES) print("Service Account Key Valid.") print(f"Email: {creds.service_account_email}") check_gsc(creds) check_gtm(creds) check_ga4(creds) except Exception as e: print("Fatal Error initializing credentials:", e)