72 lines
2.7 KiB
Python
72 lines
2.7 KiB
Python
import json
|
|
from google.oauth2.credentials import Credentials
|
|
from googleapiclient.discovery import build
|
|
|
|
TOKEN_FILE = '/workspace/personal/google_user_tokens.json'
|
|
|
|
with open(TOKEN_FILE) as f:
|
|
token_data = json.load(f)
|
|
|
|
creds = Credentials(
|
|
token=token_data['token'],
|
|
refresh_token=token_data.get('refresh_token'),
|
|
token_uri=token_data.get('token_uri', 'https://oauth2.googleapis.com/token'),
|
|
client_id=token_data['client_id'],
|
|
client_secret=token_data['client_secret'],
|
|
scopes=token_data['scopes']
|
|
)
|
|
|
|
print("--- 1. Check Authenticated User ---")
|
|
try:
|
|
oauth2 = build('oauth2', 'v2', credentials=creds)
|
|
user_info = oauth2.userinfo().get().execute()
|
|
print("User:", user_info.get('email'))
|
|
except Exception as e:
|
|
print("Userinfo error:", e)
|
|
|
|
print("\n--- 2. Search Console Sites ---")
|
|
try:
|
|
gsc = build('searchconsole', 'v1', credentials=creds)
|
|
sites = gsc.sites().list().execute()
|
|
entries = sites.get('siteEntry', [])
|
|
if entries:
|
|
print(f"Total Sites: {len(entries)}")
|
|
for s in entries:
|
|
print(f" - Site: {s.get('siteUrl')} | Permission: {s.get('permissionLevel')}")
|
|
else:
|
|
print("No sites found in Search Console.")
|
|
except Exception as e:
|
|
print("Search Console error:", e)
|
|
|
|
print("\n--- 3. Google Tag Manager Accounts & Containers ---")
|
|
try:
|
|
gtm = build('tagmanager', 'v2', credentials=creds)
|
|
accounts = gtm.accounts().list().execute().get('account', [])
|
|
if accounts:
|
|
print(f"Total GTM Accounts: {len(accounts)}")
|
|
for a in accounts:
|
|
print(f" - Account: {a.get('name')} (ID: {a.get('accountId')})")
|
|
containers = gtm.accounts().containers().list(parent=a.get('path')).execute().get('container', [])
|
|
for c in containers:
|
|
print(f" * Container: {c.get('name')} (ID: {c.get('publicId')})")
|
|
else:
|
|
print("No GTM accounts found.")
|
|
except Exception as e:
|
|
print("GTM error:", e)
|
|
|
|
print("\n--- 4. Google Analytics (GA4) Accounts & Properties ---")
|
|
try:
|
|
ga_admin = build('analyticsadmin', 'v1beta', credentials=creds)
|
|
accounts = ga_admin.accounts().list().execute().get('accounts', [])
|
|
if accounts:
|
|
print(f"Total GA Accounts: {len(accounts)}")
|
|
for a in accounts:
|
|
print(f" - Account: {a.get('displayName')} ({a.get('name')})")
|
|
# list properties
|
|
props = ga_admin.properties().list(filter=f"parent:{a.get('name')}").execute().get('properties', [])
|
|
for p in props:
|
|
print(f" * Property: {p.get('displayName')} ({p.get('name')})")
|
|
else:
|
|
print("No GA accounts found via Admin API.")
|
|
except Exception as e:
|
|
print("GA Admin error:", e)
|