Skip to content

Open-source SEO, GEO, and marketing skills for AI agents.

fix(seo): request webmasters scope when minting ADC tokens

Ean Smythe24b563c80075
diff --git a/seo/seo-analysis/scripts/_gcloud.py b/seo/seo-analysis/scripts/_gcloud.py
index a9d50953..4e4f1258 100644
--- a/seo/seo-analysis/scripts/_gcloud.py
+++ b/seo/seo-analysis/scripts/_gcloud.py
@@ -25,3 +25,37 @@ def gcloud_command(args):
def gcloud_run(args, *run_args, **run_kwargs):
"""Run gcloud with subprocess.run using the portable command wrapper."""
return subprocess.run(gcloud_command(args), *run_args, **run_kwargs)
+
+
+# Search Console needs an explicitly-requested scope. Service-account ADC
+# (GOOGLE_APPLICATION_CREDENTIALS) mints a cloud-platform-only token by default,
+# which searchconsole.googleapis.com rejects with 403
+# ACCESS_TOKEN_SCOPE_INSUFFICIENT.
+GSC_SCOPES = (
+ "https://www.googleapis.com/auth/webmasters.readonly,"
+ "https://www.googleapis.com/auth/cloud-platform"
+)
+
+
+def adc_access_token(scopes=GSC_SCOPES, timeout=15):
+ """Mint an Application Default Credentials access token.
+
+ Requests `scopes` explicitly so that service-account credentials can reach
+ Search Console. User credentials that were not granted those scopes at
+ login cannot mint them, so fall back to an unscoped request rather than
+ failing outright.
+ """
+ attempts = []
+ if scopes:
+ attempts.append(
+ ["gcloud", "auth", "application-default", "print-access-token",
+ f"--scopes={scopes}"]
+ )
+ attempts.append(["gcloud", "auth", "application-default", "print-access-token"])
+
+ result = None
+ for args in attempts:
+ result = gcloud_run(args, capture_output=True, text=True, timeout=timeout)
+ if result.returncode == 0 and result.stdout.strip():
+ return result
+ return result
diff --git a/seo/seo-analysis/scripts/analyze_gsc.py b/seo/seo-analysis/scripts/analyze_gsc.py
index 64c0287f..716d2c5b 100644
--- a/seo/seo-analysis/scripts/analyze_gsc.py
+++ b/seo/seo-analysis/scripts/analyze_gsc.py
@@ -21,7 +21,7 @@ import urllib.error
from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import date, timedelta
-from _gcloud import gcloud_run
+from _gcloud import adc_access_token, gcloud_run
from _uid import portable_uid, secure_write_json
@@ -57,10 +57,7 @@ def get_quota_project():
def get_access_token():
try:
- result = gcloud_run(
- ["gcloud", "auth", "application-default", "print-access-token"],
- capture_output=True, text=True, timeout=15
- )
+ result = adc_access_token()
except FileNotFoundError:
print("ERROR: gcloud not found. Install it and authenticate:", file=sys.stderr)
print(" https://cloud.google.com/sdk/docs/install", file=sys.stderr)
diff --git a/seo/seo-analysis/scripts/list_gsc_sites.py b/seo/seo-analysis/scripts/list_gsc_sites.py
index 2277c380..682c3f31 100644
--- a/seo/seo-analysis/scripts/list_gsc_sites.py
+++ b/seo/seo-analysis/scripts/list_gsc_sites.py
@@ -9,7 +9,7 @@ import tempfile
import urllib.request
import urllib.error
-from _gcloud import gcloud_run
+from _gcloud import adc_access_token, gcloud_run
from _uid import portable_uid, secure_write_json
@@ -31,10 +31,7 @@ def get_quota_project():
def get_access_token():
try:
- result = gcloud_run(
- ["gcloud", "auth", "application-default", "print-access-token"],
- capture_output=True, text=True, timeout=15
- )
+ result = adc_access_token()
except FileNotFoundError:
print("ERROR: gcloud not found. Install it and authenticate:", file=sys.stderr)
print(" https://cloud.google.com/sdk/docs/install", file=sys.stderr)
diff --git a/seo/seo-analysis/scripts/preflight.py b/seo/seo-analysis/scripts/preflight.py
index b6475cd2..d90ec207 100644
--- a/seo/seo-analysis/scripts/preflight.py
+++ b/seo/seo-analysis/scripts/preflight.py
@@ -18,7 +18,7 @@ import subprocess
import sys
import urllib.request
-from _gcloud import gcloud_run
+from _gcloud import adc_access_token, gcloud_run
def check_python_version():
@@ -198,10 +198,7 @@ def check_adc_credentials():
the correct scopes.
"""
try:
- result = gcloud_run(
- ["gcloud", "auth", "application-default", "print-access-token"],
- capture_output=True, text=True, timeout=15,
- )
+ result = adc_access_token()
except subprocess.TimeoutExpired:
print("ERROR: gcloud timed out checking credentials. Check your network.", file=sys.stderr)
sys.exit(1)
diff --git a/seo/seo-analysis/scripts/url_inspection.py b/seo/seo-analysis/scripts/url_inspection.py
index 7b418663..b124df6c 100644
--- a/seo/seo-analysis/scripts/url_inspection.py
+++ b/seo/seo-analysis/scripts/url_inspection.py
@@ -33,7 +33,7 @@ import urllib.request
import urllib.error
from concurrent.futures import ThreadPoolExecutor, as_completed
-from _gcloud import gcloud_run
+from _gcloud import adc_access_token, gcloud_run
from _uid import portable_uid, secure_write_json
@@ -55,10 +55,7 @@ def get_quota_project():
def get_access_token():
try:
- result = gcloud_run(
- ["gcloud", "auth", "application-default", "print-access-token"],
- capture_output=True, text=True, timeout=15
- )
+ result = adc_access_token()
except FileNotFoundError:
print("ERROR: gcloud not found. Install it from https://cloud.google.com/sdk/docs/install",
file=sys.stderr)