: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(apple_search_ads): stop retrying a failed OAuth token refresh (#127507)
Tom Owerse5b74c30cfed
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/oauth.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/oauth.pyindex f5a1d6077c48..0787ccba6b20 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/oauth.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/oauth.py@@ -6,7 +6,7 @@ so the token is resolved per authentication rather than once per run."""from posthog.integrations import IntegrationRecord-from posthog.models.integration import ERROR_TOKEN_REFRESH_FAILED, OauthIntegration+from posthog.models.integration import OauthIntegrationfrom products.warehouse_sources.backend.temporal.data_imports.sources.apple_search_ads.apple_search_ads import (AppleSearchAdsAuthError,@@ -26,7 +26,14 @@ def apple_ads_access_token(integration: IntegrationRecord) -> str:oauth_integration = OauthIntegration(integration)if oauth_integration.access_token_expired():oauth_integration.refresh_access_token()- if integration.errors == ERROR_TOKEN_REFRESH_FAILED:+ # `refresh_terminal`, not `integration.errors`, because a failed refresh also sets+ # `errors` for a transient provider hiccup (network error, 429, 5xx) that a later+ # attempt clears on its own. `refresh_terminal` only latches once the grant itself is+ # confirmed dead (an unreadable secret, or an unbroken streak of `invalid_grant`+ # responses), which is the one condition only a reconnect can fix. Until then, fall+ # through and keep using the still-valid access token the conservative expiry check+ # refreshed early for.+ if integration.config.get("refresh_terminal"):raise AppleAdsOauthError(TOKEN_REFRESH_FAILED_MESSAGE)if not integration.access_token:diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.pyindex 1a53bcf88c61..06ab2e0f2bf8 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/source.py@@ -27,6 +27,8 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.apple_searvalidate_credentials as validate_apple_search_ads_credentials,)from products.warehouse_sources.backend.temporal.data_imports.sources.apple_search_ads.oauth import (+ MISSING_TOKEN_MESSAGE,+ TOKEN_REFRESH_FAILED_MESSAGE,apple_ads_access_token,)from products.warehouse_sources.backend.temporal.data_imports.sources.apple_search_ads.settings import (@@ -103,6 +105,11 @@ class AppleSearchAdsSource(def get_non_retryable_errors(self) -> dict[str, str | None]:return {+ # Raised from the OAuth path (`apple_ads_access_token`) when the stored grant's+ # refresh token is itself expired or revoked — retrying mints no new token, the+ # customer has to reconnect.+ TOKEN_REFRESH_FAILED_MESSAGE: TOKEN_REFRESH_FAILED_MESSAGE,+ MISSING_TOKEN_MESSAGE: MISSING_TOKEN_MESSAGE,"400 Client Error: Bad Request for url: https://appleid.apple.com/auth/oauth2/token": "Apple rejected the signed client secret. Check your client ID, team ID, key ID and private key.","401 Client Error: Unauthorized for url: https://appleid.apple.com/auth/oauth2/token": "Apple rejected the signed client secret. Check your client ID, team ID, key ID and private key.","401 Client Error: Unauthorized for url: https://api.ads.apple.com": "Apple rejected the access token. Your API client may have been removed. Create a new one in Apple Ads and reconnect this source.",diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads_source.pyindex 50ea194dd9f0..9b5e44f04105 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/apple_search_ads/tests/test_apple_search_ads_source.py@@ -1,3 +1,4 @@+import timefrom datetime import datefrom typing import Any, cast@@ -7,6 +8,8 @@ from unittest import mockimport requestsfrom parameterized import parameterized+from posthog.models.integration import Integration, OauthIntegration+from products.warehouse_sources.backend.facade.source_config import (SourceFieldCredentialAccountSelectConfig,SourceFieldSelectConfig,@@ -16,6 +19,12 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.apple_searAppleSearchAdsAuthError,AppleSearchAdsResumeConfig,)+from products.warehouse_sources.backend.temporal.data_imports.sources.apple_search_ads.oauth import (+ MISSING_TOKEN_MESSAGE,+ TOKEN_REFRESH_FAILED_MESSAGE,+ AppleAdsOauthError,+ apple_ads_access_token,+)from products.warehouse_sources.backend.temporal.data_imports.sources.apple_search_ads.settings import (APPLE_ADS_API_VERSION_V1,APPLE_SEARCH_ADS_API_VERSION_V5,@@ -135,6 +144,17 @@ class TestAppleSearchAdsSource:assert any(str(status) in key and host in key for key in errors)assert all(message for message in errors.values())+ @parameterized.expand(+ [+ ("token_refresh_failed", TOKEN_REFRESH_FAILED_MESSAGE),+ ("missing_access_token", MISSING_TOKEN_MESSAGE),+ ]+ )+ def test_non_retryable_errors_cover_oauth_grant_failures(self, _name: str, error_msg: str) -> None:+ # Raised by `apple_ads_access_token` when the stored Apple Ads grant can't be refreshed;+ # retrying mints no new token, so this must stop the sync rather than retry it forever.+ assert error_message_matches(error_msg, self.source.get_non_retryable_errors().keys())+@parameterized.expand([(@@ -426,3 +446,43 @@ class TestAppleSearchAdsSource:assert accounts == []mock_client.assert_not_called()+++def _apple_ads_integration(*, refreshed_seconds_ago: int, **config: Any) -> Integration:+ return Integration(+ kind="apple-ads",+ config={"expires_in": 3600, "refreshed_at": int(time.time()) - refreshed_seconds_ago, **config},+ sensitive_config={"access_token": "old-token", "refresh_token": "refresh-token"},+ )+++class TestAppleAdsAccessToken:+ def test_a_transient_refresh_failure_keeps_the_old_token(self) -> None:+ # `refresh_access_token` also sets `errors` for a network error, a 429, or a 5xx - none+ # of which say the grant is dead. Regression: this used to raise on `errors` alone, which+ # would have disabled a sync over one self-recovering blip.+ integration = _apple_ads_integration(refreshed_seconds_ago=7200)++ def fake_refresh(oauth: OauthIntegration) -> None:+ oauth.integration.errors = "TOKEN_REFRESH_FAILED"++ with mock.patch.object(OauthIntegration, "refresh_access_token", fake_refresh):+ token = apple_ads_access_token(integration)++ assert token == "old-token"++ def test_a_terminal_refresh_failure_raises_the_reconnect_error(self) -> None:+ integration = _apple_ads_integration(refreshed_seconds_ago=7200)++ def fake_refresh(oauth: OauthIntegration) -> None:+ oauth.integration.config["refresh_terminal"] = True++ with mock.patch.object(OauthIntegration, "refresh_access_token", fake_refresh):+ with pytest.raises(AppleAdsOauthError, match=TOKEN_REFRESH_FAILED_MESSAGE):+ apple_ads_access_token(integration)++ def test_no_access_token_raises(self) -> None:+ integration = Integration(kind="apple-ads", config={}, sensitive_config={})++ with pytest.raises(AppleAdsOauthError, match=MISSING_TOKEN_MESSAGE):+ apple_ads_access_token(integration)