Skip to content

Universal SEO skill for Claude Code. 26 sub-skills + 19 sub-agents covering technical SEO, E-E-A-T, schema, GEO/AEO, agent readiness (Lighthouse Agentic Browsing, WebMCP, llms.txt), backlinks, local SEO, e-commerce, international SEO, Google APIs, and PDF/Excel reporting. 9 optional extensions for live SEO data.

feat(google): use the person's own gcloud sign-in for Google APIs

Agrici Danield1ca4593d293
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a0e5f9b..8a1e823 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -33,6 +33,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
+- Google APIs can use the person's own gcloud sign-in
+ (`gcloud auth application-default login`): it is the fallback when no
+ claude-seo OAuth token or service account is configured, and
+ `CLAUDE_SEO_GOOGLE_AUTH=adc` chooses it even when a service account is set.
+ A service account often lacks access to properties the person owns. Only an
+ `authorized_user` gcloud file counts, and it refreshes with the scopes
+ granted at login.
- `dataforseo_costs.py check` returns `credentials_in_env` on every result,
so a budget approval is not read as proof the call can run.
diff --git a/plugins/seo-cockpit/README.md b/plugins/seo-cockpit/README.md
index 2474b6b..4c6c5eb 100644
--- a/plugins/seo-cockpit/README.md
+++ b/plugins/seo-cockpit/README.md
@@ -55,6 +55,7 @@ r: Refresh e: Export
```
- **The site** comes from `/config` if you set one, else from the newest `<domain>-audit/` folder here. With neither, the pane asks for it in place and remembers it for this folder.
+- **Google access** uses claude-seo's own setup. If Search Console says "no access" but you own the property, your service account is not a user on it: either add it in Search Console, or use your own Google account with `gcloud auth application-default login --scopes=https://www.googleapis.com/auth/webmasters.readonly,https://www.googleapis.com/auth/cloud-platform` and start Claude Code with `CLAUDE_SEO_GOOGLE_AUTH=adc`. Core Web Vitals needs `GOOGLE_API_KEY`.
- **Everything loads on open**, from free sources only (the audit and grid files, the cost ledger, and Search Console and CrUX through claude-seo's own Google setup). The last result shows at once and is replaced when the fresh one arrives.
- **Missing data stays visible** as a dim line saying what to do, instead of an error screen.
- **A row opens its detail** (Enter, a click, or its number): charts and tables for that source, `b` to go back.
diff --git a/scripts/google_auth.py b/scripts/google_auth.py
index 0dfc2c6..a413b0b 100644
--- a/scripts/google_auth.py
+++ b/scripts/google_auth.py
@@ -176,6 +176,57 @@ def get_service_account_credentials(scopes: list):
return None
+def _gcloud_adc_path() -> str:
+ """Where `gcloud auth application-default login` writes the user's credentials."""
+ config_dir = os.environ.get("CLOUDSDK_CONFIG")
+ if not config_dir:
+ if sys.platform == "win32" and os.environ.get("APPDATA"):
+ config_dir = os.path.join(os.environ["APPDATA"], "gcloud")
+ else:
+ config_dir = os.path.expanduser("~/.config/gcloud")
+ return os.path.join(config_dir, "application_default_credentials.json")
+
+
+def _load_gcloud_adc() -> Optional[dict]:
+ """The gcloud user sign-in, if one exists. Only `authorized_user` counts:
+ a service account file here is not the person's own access."""
+ path = _gcloud_adc_path()
+ try:
+ with open(path, "r") as f:
+ data = json.load(f)
+ except (OSError, json.JSONDecodeError):
+ return None
+ if data.get("type") != "authorized_user" or not data.get("refresh_token"):
+ return None
+ return data
+
+
+def _prefers_gcloud() -> bool:
+ """CLAUDE_SEO_GOOGLE_AUTH=adc picks the gcloud sign-in even when a service
+ account is configured (which may not have access to every property)."""
+ return os.environ.get("CLAUDE_SEO_GOOGLE_AUTH", "").strip().lower() in ("adc", "gcloud")
+
+
+def get_gcloud_credentials(scopes: list):
+ """Credentials from the person's own gcloud sign-in
+ (`gcloud auth application-default login`), or None.
+
+ Read from the gcloud file directly, never through google.auth.default(),
+ which would load GOOGLE_APPLICATION_CREDENTIALS (a service account) first.
+ """
+ if _load_gcloud_adc() is None:
+ return None
+ try:
+ from google.oauth2.credentials import Credentials
+ # Refresh with the scopes granted at `gcloud auth application-default login`:
+ # asking for others (say, full webmasters over webmasters.readonly) fails
+ # with invalid_scope. `scopes` is accepted for symmetry and not sent.
+ return Credentials.from_authorized_user_file(_gcloud_adc_path())
+ except Exception as e:
+ print(f"gcloud sign-in could not be used: {type(e).__name__}", file=sys.stderr)
+ return None
+
+
def _load_oauth_client(creds_path: str) -> Optional[dict]:
"""Load OAuth client credentials from a client_secret JSON file."""
try:
@@ -323,6 +374,12 @@ def get_oauth_credentials(scopes: list):
"""
config = load_config()
+ # An explicit choice of the person's own gcloud sign-in wins.
+ if _prefers_gcloud():
+ creds = get_gcloud_credentials(scopes)
+ if creds is not None:
+ return creds
+
# Try OAuth token first
token_data = _load_oauth_token()
if token_data and token_data.get("access_token"):
@@ -357,8 +414,8 @@ def get_oauth_credentials(scopes: list):
except ImportError:
print("Error: google-auth required. Install with: pip install google-auth", file=sys.stderr)
- # Fall back to service account
- return get_service_account_credentials(scopes)
+ # Fall back to service account, then to the person's gcloud sign-in.
+ return get_service_account_credentials(scopes) or get_gcloud_credentials(scopes)
def run_oauth_flow(creds_path: str):
@@ -591,7 +648,12 @@ def check_credentials(service: str) -> dict:
elif SERVICE_AUTH.get(service) == "oauth_or_sa":
# Check OAuth token first
token_data = _load_oauth_token()
- if token_data and token_data.get("access_token"):
+ if _prefers_gcloud() and _load_gcloud_adc() is not None:
+ # Chosen with CLAUDE_SEO_GOOGLE_AUTH=adc: used first, so reported first.
+ result["available"] = True
+ result["method"] = "gcloud_adc"
+ result["account"] = _load_gcloud_adc().get("account")
+ elif token_data and token_data.get("access_token"):
result["available"] = True
result["method"] = "oauth_token"
expired = time.time() > token_data.get("expires_at", 0) - 60
@@ -603,7 +665,11 @@ def check_credentials(service: str) -> dict:
else:
# Fall back to service account
sa_path = config.get("service_account_path")
- if not sa_path:
+ if not sa_path and _load_gcloud_adc() is not None:
+ result["available"] = True
+ result["method"] = "gcloud_adc"
+ result["account"] = _load_gcloud_adc().get("account")
+ elif not sa_path:
result["error"] = (
"No OAuth token or service account found. Either:\n"
" 1. Run: python scripts/google_auth.py --auth --creds /path/to/client_secret.json\n"
@@ -728,6 +794,12 @@ def print_setup_instructions():
Google SEO API Setup Instructions
=================================
+FASTEST, FOR PROPERTIES YOU OWN: use your own Google account
+ gcloud auth application-default login \\
+ --scopes=https://www.googleapis.com/auth/webmasters.readonly,https://www.googleapis.com/auth/cloud-platform
+ Then set CLAUDE_SEO_GOOGLE_AUTH=adc (needed only if a service account is
+ also configured). Add an API key (step 3) for PageSpeed and CrUX.
+
1. CREATE A GOOGLE CLOUD PROJECT
- Go to https://console.cloud.google.com
- Create a new project (or select existing)
diff --git a/tests/test_google_gcloud_adc.py b/tests/test_google_gcloud_adc.py
new file mode 100644
index 0000000..598df69
--- /dev/null
+++ b/tests/test_google_gcloud_adc.py
@@ -0,0 +1,70 @@
+"""claude-seo can use the person's own gcloud sign-in for Google APIs.
+
+A service account (GOOGLE_APPLICATION_CREDENTIALS) may lack access to a
+property the person owns; their `gcloud auth application-default login`
+account does have it. These tests never touch the network.
+"""
+
+from __future__ import annotations
+
+import json
+import sys
+from pathlib import Path
+
+import pytest
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "scripts"))
+import google_auth # noqa: E402
+
+
+def _adc(tmp_path: Path, kind: str = "authorized_user") -> Path:
+ data = {"type": kind, "client_id": "id", "client_secret": "s", "refresh_token": "r", "account": "[email protected]"}
+ (tmp_path / "application_default_credentials.json").write_text(json.dumps(data))
+ return tmp_path
+
+
+def isolated(tmp_path, monkeypatch):
+ monkeypatch.setattr(google_auth, "CONFIG_PATH", str(tmp_path / "google-api.json"))
+ monkeypatch.setattr(google_auth, "TOKEN_PATH", str(tmp_path / "oauth-token.json"))
+ for name in ("GOOGLE_APPLICATION_CREDENTIALS", "CLAUDE_SEO_GOOGLE_AUTH", "GOOGLE_API_KEY"):
+ monkeypatch.delenv(name, raising=False)
+ gcloud = tmp_path / "gcloud"
+ gcloud.mkdir()
+ monkeypatch.setenv("CLOUDSDK_CONFIG", str(gcloud))
+ return gcloud
+
+
+def test_gcloud_user_sign_in_is_found(isolated):
+ _adc(isolated)
+ assert google_auth._load_gcloud_adc()["account"] == "[email protected]"
+
+
+def test_a_service_account_in_the_gcloud_file_is_not_the_person(isolated):
+ _adc(isolated, kind="service_account")
+ assert google_auth._load_gcloud_adc() is None
+
+
+def test_gcloud_is_the_fallback_when_nothing_else_is_configured(isolated):
+ _adc(isolated)
+ creds = google_auth.get_oauth_credentials(["https://www.googleapis.com/auth/webmasters.readonly"])
+ assert creds is not None and creds.refresh_token == "r"
+ check = google_auth.check_credentials("gsc")
+ assert check["available"] and check["method"] == "gcloud_adc" and check["account"] == "[email protected]"
+
+
+def test_explicit_choice_beats_a_configured_service_account(isolated, monkeypatch, tmp_path):
+ _adc(isolated)
+ sa = tmp_path / "sa.json"
+ sa.write_text(json.dumps({"type": "service_account", "client_email": "[email protected]", "private_key": "k"}))
+ monkeypatch.setenv("GOOGLE_APPLICATION_CREDENTIALS", str(sa))
+ assert google_auth.check_credentials("gsc")["method"] == "service_account"
+ monkeypatch.setenv("CLAUDE_SEO_GOOGLE_AUTH", "adc")
+ assert google_auth.check_credentials("gsc")["method"] == "gcloud_adc"
+ creds = google_auth.get_oauth_credentials(["https://www.googleapis.com/auth/webmasters.readonly"])
+ assert creds is not None and getattr(creds, "refresh_token", None) == "r"
+
+
+def test_no_sign_in_anywhere_still_explains_the_options(isolated):
+ check = google_auth.check_credentials("gsc")
+ assert not check["available"] and "service account" in check["error"]