: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(bing-ads): surface tailored error for tenant missing service principal (#60828)
Jaydeep pipaliyaa043d20caf29
diff --git a/posthog/temporal/data_imports/sources/bing_ads/source.py b/posthog/temporal/data_imports/sources/bing_ads/source.pyindex 74e236801a4c..ac79ace73d8e 100644--- a/posthog/temporal/data_imports/sources/bing_ads/source.py+++ b/posthog/temporal/data_imports/sources/bing_ads/source.py@@ -43,7 +43,20 @@ class BingAdsSource(ResumableSource[BingAdsSourceConfig, BingAdsResumeConfig], O"PostHog could not authenticate with Bing Ads. The connected account's OAuth credentials ""are revoked, expired, or no longer have access. Please reconnect your Bing Ads integration.")+ # Specific Azure AD error code surfaced when the tenant lacks a service principal for the+ # Microsoft Advertising API application. Reconnecting won't help — the org admin has to consent+ # on behalf of the tenant — so the generic "reconnect your integration" message is misleading.+ # Must be matched first: the SDK wraps this as `OAuthTokenRequestException: invalid_client AADSTS650052: …`,+ # so both "OAuthTokenRequestException" and "invalid_client" are substrings of the same message —+ # handle_non_retryable picks the first matching dict entry, so AADSTS650052 has to come before both.+ service_principal_friendly = (+ "Your Microsoft tenant has not consented to PostHog's Bing Ads connector "+ "(error AADSTS650052: missing service principal for the Microsoft Advertising API). "+ "Ask a Microsoft 365 administrator to grant admin consent to the application for your tenant, "+ "then reconnect your Bing Ads integration."+ )return {+ "AADSTS650052": service_principal_friendly,# OAuth grant rejection by Microsoft (the bingads SDK raises OAuthTokenRequestException# whose str() format is "<error_code> <error_description>")."OAuthTokenRequestException": auth_friendly,diff --git a/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads_source.py b/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads_source.pyindex bae96488cb2f..3a04da28b2e8 100644--- a/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads_source.py+++ b/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads_source.py@@ -253,6 +253,12 @@ class TestBingAdsSource:"InvalidCredentials","Failed to fetch customer ID: WebFault: ... InvalidCredentials ...",),+ # Specific Azure AD code — tenant missing service principal for the Microsoft Advertising API.+ (+ "AADSTS650052",+ "Failed to fetch customer ID: OAuthTokenRequestException: invalid_client AADSTS650052: "+ "The app is trying to access a service that your organization lacks a service principal for.",+ ),# Deterministic credential/config errors raised in source_for_pipeline.("Bing Ads access token not found", "Bing Ads access token not found for job abc"),("Bing Ads refresh token not found", "Bing Ads refresh token not found for job abc"),@@ -281,6 +287,28 @@ class TestBingAdsSource:assert not any(pattern in transient_message for pattern in non_retryable_errors)+ def test_aadsts650052_message_wins_over_generic_auth_wrappers(self):+ # The real error string contains "OAuthTokenRequestException", "invalid_client", AND "AADSTS650052"+ # as substrings. handle_non_retryable in external_data_job.py picks the first matching dict entry,+ # so AADSTS650052 must come before BOTH generic wrappers — otherwise the user sees the+ # "reconnect your integration" toast instead of the service-principal guidance, and reconnecting+ # cannot fix it (only an org admin granting tenant consent can).+ non_retryable_errors = self.source.get_non_retryable_errors()+ keys = list(non_retryable_errors.keys())++ aadsts_index = keys.index("AADSTS650052")+ assert aadsts_index < keys.index("OAuthTokenRequestException")+ assert aadsts_index < keys.index("invalid_client")++ error_message = (+ "Failed to fetch customer ID: OAuthTokenRequestException: invalid_client AADSTS650052: "+ "The app is trying to access a service that your organization lacks a service principal for."+ )+ friendly_errors = [msg for pattern, msg in non_retryable_errors.items() if pattern in error_message]+ assert friendly_errors[0] is not None+ assert "AADSTS650052" in friendly_errors[0]+ assert "service principal" in friendly_errors[0]+def test_get_resumable_source_manager(self):"""Test that get_resumable_source_manager returns a manager that round-trips BingAdsResumeConfig."""inputs = mock.MagicMock()