#!/usr/bin/env python3 import os import sys import json from googleapiclient.discovery import build from google.oauth2.credentials import Credentials TOKEN_PATH = "/root/.hermes/profiles/reviewer/google_token.json" def test_search_console(): print("=== GOOGLE SEARCH CONSOLE PROPERTIES ===") try: creds = Credentials.from_authorized_user_file(TOKEN_PATH) # Search Console API (webmasters service v3) service = build("webmasters", "v3", credentials=creds) sites = service.sites().list().execute() site_list = sites.get("siteEntry", []) if not site_list: print("Không tìm thấy website nào được đăng ký trên Search Console.") else: for s in site_list: print(f"- Site: {s.get('siteUrl')} (Permission: {s.get('permissionLevel')})") except Exception as e: print(f"Lỗi truy cập Search Console: {e}") def test_analytics(): print("\n=== GOOGLE ANALYTICS PROPERTIES ===") try: creds = Credentials.from_authorized_user_file(TOKEN_PATH) # Analytics Admin API v1beta for GA4 properties service = build("analyticsadmin", "v1beta", credentials=creds) # List accounts accounts = service.accounts().list().execute() account_list = accounts.get("accounts", []) if not account_list: print("Không tìm thấy tài khoản Analytics nào.") return for acc in account_list: acc_name = acc.get("name") display_name = acc.get("displayName") print(f"- Account: {display_name} ({acc_name})") # List properties under this account properties = service.properties().list(filter=f"parent:{acc_name}").execute() prop_list = properties.get("properties", []) for prop in prop_list: print(f" * Property: {prop.get('displayName')} ({prop.get('name')})") except Exception as e: print(f"Lỗi truy cập Google Analytics: {e}") if __name__ == "__main__": if not os.path.exists(TOKEN_PATH): print(f"Không tìm thấy file token tại {TOKEN_PATH}") sys.exit(1) test_search_console() test_analytics()