Our sourdough page wasn’t showing up on Google, and we had no idea why. Our new tutorial covers basic SEO, Google Search Console, and how to give an AI agent read-only access with a service account:
Here’s the companion code for the article. It’s roughly what an agent writes when you hand it the prompt from the article: a short Python script that signs in with a service account key, asks for read-only access, and pulls the last 28 days of Search Console data by page and query.
First, install Google’s client libraries:
pip install google-auth google-api-python-client
Then swap in your own key file and property, and run it:
import os
from datetime import date, timedelta
from google.oauth2 import service_account
from googleapiclient.discovery import build
KEY_FILE = os.path.expanduser("~/secrets/search-console-key.json")
PROPERTY = "sc-domain:example.com" # or "https://example.com/" for a URL-prefix property
creds = service_account.Credentials.from_service_account_file(
KEY_FILE, scopes=["https://www.googleapis.com/auth/webmasters.readonly"])
search_console = build("searchconsole", "v1", credentials=creds)
end = date.today() - timedelta(days=3) # the newest data takes a few days to settle
start = end - timedelta(days=27) # 28 days in all
report = search_console.searchanalytics().query(
siteUrl=PROPERTY,
body={"startDate": start.isoformat(), "endDate": end.isoformat(),
"dimensions": ["page", "query"], "rowLimit": 25000}).execute()
for row in report.get("rows", []):
page, query = row["keys"]
print(page, query, row["clicks"], row["impressions"], round(row["position"], 1))
Rows come back sorted by clicks, so it asks for 25,000 rows (the most one request allows) to make sure the searches with lots of impressions and few clicks make the cut. The service account’s email needs to be a user on the property in Search Console, or Google answers with a 403.