: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(google-ads): say how to fix a missing Google Ads OAuth scope (#104237)
Tom Owers2a8aeebd14c3
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.pyindex 06e8d6d2dfa3..40b146bee833 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/source.py@@ -82,6 +82,15 @@ def _oauth_accounts_cache_key(team_id: int, integration_id: int) -> str:return f"@dwh/google_ads/{team_id}/{integration_id}/oauth_accounts"+# The connected Google login granted PostHog an OAuth token without the adwords scope, so+# nothing it asks for will be authorized. The wizard and the sync both surface this, and+# reconnecting is the only fix, so both paths read from one string.+_SCOPE_INSUFFICIENT_ERROR = (+ "Your Google Ads connection is missing the access PostHog needs. Reconnect your Google Ads "+ "account and allow access to your Google Ads data."+)++@SourceRegistry.registerclass GoogleAdsSource(ResumableSource[GoogleAdsSourceConfig | GoogleAdsServiceAccountSourceConfig, GoogleAdsResumeConfig], OAuthMixin@@ -118,7 +127,7 @@ class GoogleAdsSource(# `PERMISSION_DENIED` / `UNAUTHENTICATED` gRPC statuses. Specific codes therefore come first,# so a scope or deleted-account failure doesn't get the generic access message.return {- "ACCESS_TOKEN_SCOPE_INSUFFICIENT": "Your Google Ads connection is missing the access PostHog needs. Reconnect your Google Ads account and allow access to your Google Ads data.",+ "ACCESS_TOKEN_SCOPE_INSUFFICIENT": _SCOPE_INSUFFICIENT_ERROR,"Account has been deleted": "The Google Ads account this source syncs from has been deleted, so there's nothing left to import. Point the source at an active customer ID, or delete the source.","INVALID_CUSTOMER_ID": "The customer ID on this source isn't a valid Google Ads account. Update it to the 10-digit customer ID shown in your Google Ads account, then re-enable the sync.","REQUESTED_METRICS_FOR_MANAGER": "Metrics cannot be requested for a Google Ads manager (MCC) account. Reconfigure this source with a client account customer ID, or enable the MCC option and provide both the manager and client customer IDs.",@@ -515,10 +524,7 @@ class GoogleAdsSource(except Exception as e:error_message = str(e)if "ACCESS_TOKEN_SCOPE_INSUFFICIENT" in error_message:- return (- False,- "Insufficient permissions. Please reconnect your Google Ads account with the required scopes.",- )+ return False, _SCOPE_INSUFFICIENT_ERRORif "NOT_ADS_USER" in error_message:return (False,diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.pyindex ca74f399d952..9375e2c78821 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_ads/tests/test_google_ads_source.py@@ -496,6 +496,25 @@ class TestValidateCredentials:assert "216.239.36.223" not in (message or "")assert "StatusCode" not in (message or "")+ def test_insufficient_scope_tells_the_user_to_reconnect(self):+ # Google raises this when the stored OAuth token was granted without the adwords scope.+ # A user cannot act on "the required scopes" because they never choose scopes, so the+ # wizard has to ask for the same reconnect the sync path asks for.+ config = GoogleAdsSourceConfig(customer_id="1234567890", google_ads_integration_id=1)+ client = mock.Mock()+ client.get_service.return_value.list_accessible_customers.side_effect = Exception(+ "ACCESS_TOKEN_SCOPE_INSUFFICIENT: Request had insufficient authentication scopes"+ )+ with mock.patch(+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_ads.google_ads.google_ads_client",+ return_value=client,+ ):+ ok, message = GoogleAdsSource().validate_credentials(config, team_id=1)++ assert ok is False+ assert "Reconnect your Google Ads account" in (message or "")+ assert "scopes" not in (message or "")+def test_transient_google_side_error_returns_retry_message(self):# A transient INTERNAL/UNAVAILABLE blip from Google stringifies as a raw gRPC status plus a# protobuf failure dump. Surface a clean retry prompt instead of leaking that to the wizard.diff --git a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_google_ads_source.py b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_google_ads_source.pyindex 719451e6d557..bff5ce838747 100644--- a/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_google_ads_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/tests/e2e/test_google_ads_source.py@@ -468,7 +468,7 @@ class TestGoogleAdsSourceValidation:assert is_valid is Falseassert error is not None- assert "Insufficient permissions" in error+ assert "Reconnect your Google Ads account" in error@mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.google_ads.google_ads.google_ads_client"