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(bing_ads): fail fast when OAuth app credentials are unconfigured (#64445)

Tom Owers948c1511e74d
diff --git a/posthog/temporal/data_imports/sources/bing_ads/bing_ads.py b/posthog/temporal/data_imports/sources/bing_ads/bing_ads.py
index 38368cc1a249..987bc1b861dd 100644
--- a/posthog/temporal/data_imports/sources/bing_ads/bing_ads.py
+++ b/posthog/temporal/data_imports/sources/bing_ads/bing_ads.py
@@ -81,6 +81,12 @@ def bing_ads_source(
if not developer_token:
raise ValueError("Bing Ads developer token not configured")
+ # Without these the SDK posts a token request omitting client_id, and Microsoft replies with the
+ # opaque AADSTS900144 ("request body must contain client_id") — fail fast so it isn't mis-surfaced
+ # as a customer "reconnect your integration" error when it's really a missing PostHog config.
+ if not integrations.BING_ADS_CLIENT_ID or not integrations.BING_ADS_CLIENT_SECRET:
+ raise ValueError("Bing Ads OAuth application credentials not configured")
+
client = BingAdsClient(
access_token=access_token,
refresh_token=refresh_token,
diff --git a/posthog/temporal/data_imports/sources/bing_ads/source.py b/posthog/temporal/data_imports/sources/bing_ads/source.py
index feec7db38962..d91b73bd4818 100644
--- a/posthog/temporal/data_imports/sources/bing_ads/source.py
+++ b/posthog/temporal/data_imports/sources/bing_ads/source.py
@@ -106,6 +106,9 @@ class BingAdsSource(ResumableSource[BingAdsSourceConfig, BingAdsResumeConfig], O
"Bing Ads access token not found": "Bing Ads OAuth access token is missing. Please reconnect your Bing Ads integration.",
"Bing Ads refresh token not found": "Bing Ads OAuth refresh token is missing. Please reconnect your Bing Ads integration.",
"Bing Ads developer token not configured": None,
+ # PostHog's Bing Ads OAuth application credentials aren't configured — an empty client_id makes
+ # Microsoft reject the token request with AADSTS900144. Internal config, not customer-actionable.
+ "Bing Ads OAuth application credentials not configured": None,
}
@property
diff --git a/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads.py b/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads.py
index 454fde21ad38..195e159785ca 100644
--- a/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads.py
+++ b/posthog/temporal/data_imports/sources/bing_ads/tests/test_bing_ads.py
@@ -342,6 +342,37 @@ class TestBingAdsSource:
assert isinstance(items, Iterable)
list(items)
+ @parameterized.expand(
+ [
+ ("missing_client_id", "", "test_client_secret"),
+ ("missing_client_secret", "test_client_id", ""),
+ ("both_missing", "", ""),
+ ]
+ )
+ @patch("posthog.temporal.data_imports.sources.bing_ads.bing_ads.integrations")
+ def test_bing_ads_source_missing_oauth_app_credentials(self, _name, client_id, client_secret, mock_integrations):
+ """An empty OAuth app client id/secret raises a deterministic error instead of a doomed token request.
+
+ Without the guard the SDK posts a token request omitting client_id and Microsoft returns the opaque
+ AADSTS900144, which was being mis-surfaced as a customer "reconnect your integration" error.
+ """
+ mock_integrations.BING_ADS_DEVELOPER_TOKEN = "test_dev_token"
+ mock_integrations.BING_ADS_CLIENT_ID = client_id
+ mock_integrations.BING_ADS_CLIENT_SECRET = client_secret
+
+ result = bing_ads_source(
+ account_id=self.account_id,
+ resource_name="campaigns",
+ access_token=self.access_token,
+ refresh_token=self.refresh_token,
+ resumable_source_manager=_mock_resumable_manager(),
+ )
+
+ with pytest.raises(ValueError, match="Bing Ads OAuth application credentials not configured"):
+ items = result.items()
+ assert isinstance(items, Iterable)
+ list(items)
+
@parameterized.expand(
[
("account_number", "F118FDGN"),
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.py
index 560312d63990..712be478c582 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
@@ -288,6 +288,10 @@ class TestBingAdsSource:
("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"),
("Bing Ads developer token not configured", "Bing Ads developer token not configured"),
+ (
+ "Bing Ads OAuth application credentials not configured",
+ "Bing Ads OAuth application credentials not configured",
+ ),
],
)
def test_get_non_retryable_errors_pattern_recognised(self, pattern, raised_message):