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-analytics): surface a clear error on OAuth token refresh failure (#69192)

Tom Owers85325d3a3069
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.py
index f52fbb4e8510..458fa9f36d94 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/source.py
@@ -1,6 +1,7 @@
from typing import Optional, cast
import requests
+from google.auth.exceptions import RefreshError
from posthog.schema import (
DataWarehouseSourceCategory,
@@ -145,6 +146,17 @@ class GoogleAnalyticsSource(ResumableSource[GoogleAnalyticsSourceConfig, GoogleA
"Google Analytics admin settings.",
)
return False, f"Failed to read Google Analytics property metadata: {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 Analytics. Your connection may have "
+ "expired or is missing the required permissions. Please reconnect your Google "
+ "account and grant access to Google Analytics.",
+ )
except Exception as e:
return False, f"Failed to read Google Analytics property metadata: {e}"
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py
index 7fdaaa93b75d..aec9531345db 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_analytics/tests/test_google_analytics_source.py
@@ -2,6 +2,7 @@ import pytest
from unittest import mock
import requests
+from google.auth.exceptions import RefreshError
from posthog.schema import ReleaseStatus, SourceFieldOauthConfig
@@ -178,6 +179,26 @@ def test_validate_credentials_maps_http_errors(status_code, expected_substring):
assert expected_substring in (message or "")
+def test_validate_credentials_maps_token_refresh_error():
+ # google-auth raises RefreshError with (message, response_dict); its default repr is the tuple,
+ # which used to leak verbatim to users. Guard the mapping to a clean reconnect prompt.
+ refresh_error = RefreshError("invalid_scope: Bad Request", {"error": "invalid_scope"})
+ with (
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_analytics.source.google_analytics_session"
+ ),
+ mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_analytics.source.get_property_metadata",
+ side_effect=refresh_error,
+ ),
+ ):
+ ok, message = GoogleAnalyticsSource().validate_credentials(_config(), team_id=1)
+
+ assert ok is False
+ assert "reconnect your Google" in (message or "")
+ assert "invalid_scope" not in (message or "")
+
+
def test_validate_credentials_handles_session_failure():
with mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.google_analytics.source.google_analytics_session",