: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): recognize revoked-token wording under generic 40000 code (#121766)
Tom Owersb03cd6d5c445
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 8a3ff0c41563..41eac94152fd 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@@ -43,6 +43,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.tiktok_adsTIKTOK_AUTH_ERROR_CODES,TIKTOK_CREATIVE_PERMISSION_DENIED_FRAGMENTS,TIKTOK_CREATIVE_PERMISSION_DENIED_MESSAGE,+ TIKTOK_INVALID_TOKEN_MESSAGE,TIKTOK_MISSING_SCOPE_FRAGMENT,TIKTOK_MISSING_SCOPE_MESSAGE,TIKTOK_NON_RETRYABLE_ERROR_PREFIX,@@ -201,7 +202,8 @@ class TikTokAdsSource(ResumableSource[TikTokAdsSourceConfig, TikTokAdsResumeConfraise IntegrationAccountListingError(TIKTOK_TRANSIENT_ERROR_MESSAGE) from eexcept TikTokAdsAPIError as e:if e.api_code in TIKTOK_AUTH_ERROR_CODES or (- e.api_code == 40000 and TIKTOK_APP_TOKEN_MISMATCH_MESSAGE in str(e)+ e.api_code == 40000+ and (TIKTOK_APP_TOKEN_MISMATCH_MESSAGE in str(e) or TIKTOK_INVALID_TOKEN_MESSAGE in str(e))):raise IntegrationAccountListingError("TikTok rejected the credentials for this integration. Please reconnect your TikTok Ads "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 637d28d8453b..ad2ad2b8d1e5 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@@ -281,11 +281,13 @@ class TestTikTokAdsSource:40000,"The app_id is inconsistent with the token's app information. Please retry after correcting it.",),+ ("revoked_token", 40000, "Access token is incorrect or has been revoked."),])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."""+ """Auth-rejection responses — including the app/token mismatch and revoked-token wording+ 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 (diff --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 4c51632b6187..6f518f5706e6 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@@ -33,6 +33,11 @@ TIKTOK_AUTH_ERROR_CODES = {40105, 40110}# 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"+# Also under the generic 40000 bucket: TikTok's wording for a revoked/invalid token on this+# endpoint, distinct from the 40105 code `list_advertisers` otherwise relies on. Reconnecting is+# the only fix.+TIKTOK_INVALID_TOKEN_MESSAGE = "Access token is incorrect or has been revoked"+# 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.