Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(google-search-console): surface clear errors during credential validation (#63526)

Tom Owers7aed675dc7da
diff --git a/posthog/temporal/data_imports/sources/google_search_console/source.py b/posthog/temporal/data_imports/sources/google_search_console/source.py
index d19e1eba20a9..88932633404a 100644
--- a/posthog/temporal/data_imports/sources/google_search_console/source.py
+++ b/posthog/temporal/data_imports/sources/google_search_console/source.py
@@ -1,6 +1,7 @@
from typing import Optional, cast
import requests
+from google.auth.exceptions import RefreshError
from posthog.schema import (
DataWarehouseSourceCategory,
@@ -12,6 +13,7 @@ from posthog.schema import (
SourceFieldOauthConfig,
)
+from posthog.models.integration import Integration
from posthog.temporal.data_imports.pipelines.pipeline.typings import SourceInputs, SourceResponse
from posthog.temporal.data_imports.sources.common.base import FieldType, ResumableSource
from posthog.temporal.data_imports.sources.common.mixins import OAuthMixin
@@ -109,6 +111,11 @@ class GoogleSearchConsoleSource(
) -> tuple[bool, str | None]:
try:
session = google_search_console_session(config.google_search_console_integration_id, team_id)
+ except Integration.DoesNotExist:
+ return (
+ False,
+ "The Google Search Console connection for this source no longer exists. Please reconnect your Google account.",
+ )
except Exception as e:
if "matching query does not exist" in str(e):
return False, (
@@ -127,6 +134,17 @@ class GoogleSearchConsoleSource(
"Google Search Console rejected the credentials. Please reconnect your account and ensure it has read access to the property.",
)
return False, f"Failed to list Google Search Console sites: {e}"
+ except RefreshError:
+ # Raised while AuthorizedSession refreshes the OAuth access token (e.g. invalid_scope or
+ # invalid_grant): the stored token is missing the required permissions, or has expired or
+ # been revoked. Retrying can't recover it — the raw RefreshError repr is meaningless to
+ # users, so guide them to reconnect.
+ return (
+ False,
+ "PostHog could not authenticate with Google Search Console. Your connection may have "
+ "expired or is missing the required permissions. Please reconnect your Google account "
+ "and grant access to Search Console.",
+ )
except Exception as e:
return False, f"Failed to list Google Search Console sites: {e}"
diff --git a/posthog/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.py b/posthog/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.py
index b0d277c063dd..09fac726c374 100644
--- a/posthog/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.py
+++ b/posthog/temporal/data_imports/sources/google_search_console/tests/test_google_search_console_source.py
@@ -121,6 +121,51 @@ def test_validate_credentials_handles_auth_failures(status_code, expected_substr
assert expected_substring in (message or "")
+def test_validate_credentials_missing_integration_returns_reconnect_message():
+ from posthog.models.integration import Integration
+
+ with mock.patch(
+ "posthog.temporal.data_imports.sources.google_search_console.source.google_search_console_session",
+ side_effect=Integration.DoesNotExist(),
+ ):
+ ok, message = GoogleSearchConsoleSource().validate_credentials(_config(), team_id=1)
+
+ assert ok is False
+ assert "no longer exists" in (message or "")
+ assert "Integration matching query" not in (message or "")
+
+
+ "error_args,banned_substring",
+ [
+ (
+ ("invalid_scope: Bad Request", {"error": "invalid_scope", "error_description": "Bad Request"}),
+ "invalid_scope",
+ ),
+ (
+ ("invalid_grant: Token has been expired or revoked.", {"error": "invalid_grant"}),
+ "invalid_grant",
+ ),
+ ],
+)
+def test_validate_credentials_refresh_error_returns_reconnect_message(error_args, banned_substring):
+ from google.auth.exceptions import RefreshError
+
+ err = RefreshError(*error_args)
+ with (
+ mock.patch("posthog.temporal.data_imports.sources.google_search_console.source.google_search_console_session"),
+ mock.patch(
+ "posthog.temporal.data_imports.sources.google_search_console.source.list_sites",
+ side_effect=err,
+ ),
+ ):
+ ok, message = GoogleSearchConsoleSource().validate_credentials(_config(), team_id=1)
+
+ assert ok is False
+ assert "reconnect your Google account" in (message or "")
+ assert banned_substring not in (message or "")
+
+
def test_validate_credentials_rejects_unknown_site():
with (
mock.patch("posthog.temporal.data_imports.sources.google_search_console.source.google_search_console_session"),