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(integrations): give a clear error when TikTok Ads OAuth grants no ad account (#83459)

Tom Owers837bf6998e08
diff --git a/posthog/models/integration.py b/posthog/models/integration.py
index 989c61937c94..b2dd4ce9e8f5 100644
--- a/posthog/models/integration.py
+++ b/posthog/models/integration.py
@@ -1713,6 +1713,15 @@ class OauthIntegration:
# token to cover these scopes before it will wield the connection's grant.
config["granted_scopes"] = sorted({s for s in (config.get("scope") or "").split() if ":" in s})
+ # TikTok can complete OAuth without the user granting any advertiser account, leaving
+ # `advertiser_ids` empty. Surface an actionable reconnect message rather than the generic
+ # "failed to extract integration ID" 500 the guard below would otherwise raise.
+ if kind == "tiktok-ads" and isinstance(integration_id, list) and len(integration_id) == 0:
+ raise ValidationError(
+ "No TikTok ad accounts were authorized. In TikTok, grant access to at least one "
+ "advertiser account, then reconnect."
+ )
+
if isinstance(integration_id, int):
integration_id = str(integration_id)
elif isinstance(integration_id, list) and len(integration_id) > 0:
diff --git a/posthog/models/test/test_integration_model.py b/posthog/models/test/test_integration_model.py
index 1fcfad08bc85..efc0c388bb07 100644
--- a/posthog/models/test/test_integration_model.py
+++ b/posthog/models/test/test_integration_model.py
@@ -535,6 +535,26 @@ class TestOauthIntegrationModel(BaseTest):
},
)
+ @patch("posthog.models.integration.requests.post")
+ def test_tiktok_ads_oauth_without_advertiser_accounts_raises_validation_error(self, mock_post):
+ # TikTok completes OAuth even when the user granted no advertiser account, leaving
+ # `advertiser_ids` empty. That must surface as a ValidationError (→ 400 with an actionable
+ # message) rather than the bare Exception (→ 500) the missing-id guard would otherwise raise.
+ with self.settings(**self.mock_settings):
+ mock_post.return_value.status_code = 200
+ mock_post.return_value.json.return_value = {
+ "code": 0,
+ "data": {"access_token": "FAKE_ACCESS_TOKEN", "advertiser_ids": []},
+ }
+
+ with pytest.raises(ValidationError, match="ad accounts"):
+ OauthIntegration.integration_from_oauth_response(
+ "tiktok-ads",
+ self.team.id,
+ self.user,
+ {"code": "code", "state": "next=/projects/test"},
+ )
+
@patch("posthog.models.integration.requests.post")
@patch("posthog.models.integration.requests.get")
def test_integration_fetches_info_from_token_info_url(self, mock_get, mock_post):