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-ads): name the missing permission in the account picker (#119595)

Tom Owers16a34d2961f2
diff --git a/posthog/models/integration/__init__.py b/posthog/models/integration/__init__.py
index 89b6b633d166..3c492e2c52fd 100644
--- a/posthog/models/integration/__init__.py
+++ b/posthog/models/integration/__init__.py
@@ -50,7 +50,7 @@ from .github import (
invalidate_github_repository_caches_for_installation,
)
from .gitlab import GitLabIntegration, GitLabIntegrationError
-from .google_ads import GoogleAdsIntegration, google_ads_hierarchy_level
+from .google_ads import GoogleAdsIntegration, GoogleAdsScopeInsufficientError, google_ads_hierarchy_level
from .google_cloud import (
GoogleCloudIntegration,
GoogleCloudServiceAccountIntegration,
@@ -198,6 +198,7 @@ __all__ = [
"validate_slack_request",
"google_ads_hierarchy_level",
"GoogleAdsIntegration",
+ "GoogleAdsScopeInsufficientError",
"is_unique_service_account_by_organization_id",
"GoogleCloudServiceAccountIntegration",
"GoogleCloudIntegration",
diff --git a/posthog/models/integration/google_ads.py b/posthog/models/integration/google_ads.py
index 449867e29b34..d860bd9c5554 100644
--- a/posthog/models/integration/google_ads.py
+++ b/posthog/models/integration/google_ads.py
@@ -23,6 +23,10 @@ def google_ads_hierarchy_level(account: dict) -> int:
return int(account.get("level") or 0)
+class GoogleAdsScopeInsufficientError(ValidationError):
+ """Google's consent screen lets a login sign in without ticking the Google Ads permission."""
+
+
@retry(
stop=stop_after_attempt(3),
wait=wait_fixed(2),
@@ -122,6 +126,11 @@ class GoogleAdsIntegration:
)
if response.status_code == 403:
+ if "ACCESS_TOKEN_SCOPE_INSUFFICIENT" in response.text:
+ raise GoogleAdsScopeInsufficientError(
+ "Your Google Ads connection is missing the access PostHog needs. Reconnect your Google Ads "
+ "account and allow access to your Google Ads data."
+ )
raise ValidationError(
"This integration does not have permission to access this resource. "
"Please check the account permissions on the provider side."
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.py
index 84738729e7b8..ed7e0cf48e62 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
@@ -10,6 +10,7 @@ from rest_framework.exceptions import ValidationError
from posthog.models.integration import (
ERROR_TOKEN_REFRESH_FAILED,
GoogleAdsIntegration,
+ GoogleAdsScopeInsufficientError,
Integration,
OauthIntegration,
google_ads_hierarchy_level,
@@ -410,6 +411,9 @@ class GoogleAdsSource(
try:
accounts = GoogleAdsIntegration(integration).list_google_ads_accessible_accounts()
+ except GoogleAdsScopeInsufficientError as e:
+ # Reconnecting alone repeats the same consent screen, so name the permission to allow.
+ raise IntegrationAccountListingError(_SCOPE_INSUFFICIENT_ERROR) from e
except ValidationError as e:
# Raised only for a 401/403 from Google: revoked credentials, or the connected account
# lost access.
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.py
index bb2f0e0663ee..266ec8787036 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
@@ -1524,6 +1524,35 @@ class TestGetOAuthAccountsNetworkErrorHandling:
source.get_oauth_accounts(1, 2)
+class TestGetOAuthAccountsPermissionErrors:
+ @pytest.mark.parametrize(
+ "body, expected",
+ [
+ (
+ '{"error": {"status": "PERMISSION_DENIED", "details": [{"reason": "ACCESS_TOKEN_SCOPE_INSUFFICIENT"}]}}',
+ "allow access to your Google Ads data",
+ ),
+ ('{"error": {"status": "PERMISSION_DENIED"}}', "Google rejected the credentials"),
+ ],
+ )
+ def test_forbidden_listing_names_the_fix(self, body, expected):
+ cache.clear()
+ integration = mock.Mock(errors=None, kind="google-ads", sensitive_config={"access_token": "token"})
+
+ with (
+ mock.patch.object(GoogleAdsSource, "get_oauth_integration", return_value=integration),
+ mock.patch(f"{_SOURCE_MODULE}.OauthIntegration") as mock_oauth,
+ mock.patch(
+ "posthog.models.integration.google_ads.requests.request",
+ return_value=mock.Mock(status_code=403, text=body),
+ ),
+ ):
+ mock_oauth.return_value.access_token_expired.return_value = False
+
+ with pytest.raises(IntegrationAccountListingError, match=expected):
+ GoogleAdsSource().get_oauth_accounts(1, 2)
+
+
class _DrainClock:
"""A monotonic clock the test drives itself, one second per window drained.