#!/usr/bin/env python3
"""Cek apakah SA bisa mengakses Google Business Profile API (dan API mana yang aktif)."""
import json, os, urllib.request, urllib.error
from google.auth.transport.requests import Request
from google.oauth2 import service_account

creds = service_account.Credentials.from_service_account_file(
    os.path.expanduser("~/.hermes/gsc_service_account.json"),
    scopes=["https://www.googleapis.com/auth/business.manage"])
creds.refresh(Request())
H = {"Authorization": f"Bearer {creds.token}", "User-Agent": "Mozilla/5.0"}

UJI = [
    ("Account Management v1", "https://mybusinessaccountmanagement.googleapis.com/v1/accounts"),
    ("Business Information v1", "https://mybusinessbusinessinformation.googleapis.com/v1/accounts"),
    ("Performance v1", "https://businessprofileperformance.googleapis.com/v1/locations/0"),
    ("Legacy v4.9 (local posts)", "https://mybusiness.googleapis.com/v4/accounts"),
]

for nama, url in UJI:
    try:
        with urllib.request.urlopen(urllib.request.Request(url, headers=H), timeout=40) as x:
            body = x.read().decode()[:300]
            print(f"  ✅ {nama:<26} HTTP {x.status} | {body[:160]}")
    except urllib.error.HTTPError as e:
        pesan = e.read().decode()[:300]
        print(f"  ❌ {nama:<26} HTTP {e.code} | {pesan[:200]}")
    except Exception as e:
        print(f"  ⚠️ {nama:<26} {type(e).__name__}: {str(e)[:100]}")

print("\n=== scope yang dipegang SA untuk file ini ===")
print(" ", creds.scopes)
