: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(tiktok_ads): surface app/token mismatch as a reconnect prompt (#78577)
Tom Owerse1a08c1c6c98
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.pyindex 001e94f7bbc9..1d2211bffe55 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/source.py@@ -40,6 +40,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_adstiktok_ads_source,)from products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_ads.utils import (+ TIKTOK_APP_TOKEN_MISMATCH_MESSAGE,TIKTOK_AUTH_ERROR_CODES,TIKTOK_NON_RETRYABLE_ERROR_PREFIX,TIKTOK_TRANSIENT_ERROR_CODES,@@ -153,7 +154,9 @@ class TikTokAdsSource(ResumableSource[TikTokAdsSourceConfig, TikTokAdsResumeConf# Transient and TikTok/network-side: map to the same actionable "try again" message.raise IntegrationAccountListingError(TIKTOK_TRANSIENT_ERROR_MESSAGE) from eexcept TikTokAdsAPIError as e:- if e.api_code in TIKTOK_AUTH_ERROR_CODES:+ if e.api_code in TIKTOK_AUTH_ERROR_CODES or (+ e.api_code == 40000 and TIKTOK_APP_TOKEN_MISMATCH_MESSAGE in str(e)+ ):raise IntegrationAccountListingError("TikTok rejected the credentials for this integration. Please reconnect your TikTok Ads ""integration and make sure the connected account can access your advertiser accounts."diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.pyindex 9d03d3443ff2..f832879307b6 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/test/test_tiktok_ads_source.py@@ -29,6 +29,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.generated_from products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_ads.source import TikTokAdsSourcefrom products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_ads.utils import (TIKTOK_TRANSIENT_ERROR_MESSAGE,+ TikTokAdsAPIError,TikTokAdsPaginator,)from products.warehouse_sources.backend.types import ExternalDataSourceType, IncrementalFieldType@@ -137,6 +138,44 @@ class TestTikTokAdsSource:assert str(exc_info.value) == TIKTOK_TRANSIENT_ERROR_MESSAGE+ @parameterized.expand(+ [+ ("invalid_access_token", 40105, "Invalid access_token"),+ ("no_advertiser_permission", 40110, "No permission to access this advertiser"),+ (+ "app_token_mismatch",+ 40000,+ "The app_id is inconsistent with the token's app information. Please retry after correcting it.",+ ),+ ]+ )+ def test_get_oauth_accounts_maps_auth_errors_to_reconnect_message(self, name, api_code, message):+ """Auth-rejection responses — including the app/token mismatch TikTok returns under the+ generic 40000 code — must surface as an actionable reconnect message, not escape as a bug."""+ _MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_ads.source"+ error = TikTokAdsAPIError(message, api_code=api_code)+ with (+ patch.object(self.source, "get_oauth_integration", return_value=self.mock_integration),+ patch(f"{_MODULE}.list_advertisers", side_effect=error),+ ):+ with pytest.raises(IntegrationAccountListingError) as exc_info:+ self.source.get_oauth_accounts(self.integration_id, self.team_id)++ assert "reconnect your TikTok Ads integration" in str(exc_info.value)++ def test_get_oauth_accounts_does_not_treat_other_40000_errors_as_auth(self):+ """Code 40000 is a generic params-error bucket with many unrelated messages — only the+ specific app/token mismatch text should map to the reconnect message; anything else must+ keep surfacing as a bug so real request-construction errors aren't hidden."""+ _MODULE = "products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_ads.source"+ error = TikTokAdsAPIError("Invalid parameter: advertiser_id", api_code=40000)+ with (+ patch.object(self.source, "get_oauth_integration", return_value=self.mock_integration),+ patch(f"{_MODULE}.list_advertisers", side_effect=error),+ ):+ with pytest.raises(TikTokAdsAPIError):+ self.source.get_oauth_accounts(self.integration_id, self.team_id)+def test_get_source_config(self):"""Test source configuration generation."""config = self.source.get_source_configdiff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.py b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.pyindex 254728e48bf8..a02b980f2104 100644--- a/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.py+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/tiktok_ads/utils.py@@ -27,6 +27,12 @@ logger = structlog.get_logger(__name__)# missing permission). Note 40100 is NOT one of them — it means "requests made too frequently".TIKTOK_AUTH_ERROR_CODES = {40105, 40110}+# Code 40000 is TikTok's generic "params error" bucket and covers many unrelated messages, so it+# can't be treated as auth-only by code alone. This specific message means the access token was+# minted under a different TikTok app than the one currently configured — reconnecting to mint a+# fresh token under the current app is the only fix; retrying with the same token cannot succeed.+TIKTOK_APP_TOKEN_MISMATCH_MESSAGE = "app_id is inconsistent with the token's app information"+# Throttling (400xx) and TikTok-side outages (5xxxx/60001) also arrive as HTTP 200 + a body `code`,# so no HTTP-level retry ever fires on them. Neither is our bug and neither is fixed by reconnecting:# the caller just has to try again shortly.