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(cdp): classify Meta Graph code 190 refresh failures as invalid_grant (#84546)

Harley Alexanderf92bb1f18f74
diff --git a/posthog/models/integration.py b/posthog/models/integration.py
index cfc182a48a02..83053915d4e5 100644
--- a/posthog/models/integration.py
+++ b/posthog/models/integration.py
@@ -307,6 +307,12 @@ def oauth_refresh_failure_reason(status_code: int, body: dict, kind: str | None
# `BAD_REFRESH_TOKEN` responses do carry `"error": "invalid_grant"` and need no special case.
if kind == "hubspot" and status_code < 500 and body.get("status") == "BAD_HUB":
return REFRESH_FAILURE_REASON_INVALID_GRANT
+ # Meta Graph nests its error as an object (`{"error": {"code": 190, ...}}`) and never
+ # sends the `invalid_grant` string. Code 190 means the access token is dead (password
+ # change, checkpoint, expiry, revocation). Without this mapping, a revoked Meta token
+ # classifies as `other`. Meta rate limits use codes 4, 17, and 32, which do not match.
+ if kind in ("meta-ads", "instagram") and status_code < 500 and isinstance(error, dict) and error.get("code") == 190:
+ return REFRESH_FAILURE_REASON_INVALID_GRANT
# Transient throttling, not a credential problem: the backoff cap synchronises failed
# integrations into retry herds that can trip a provider's per-second limit and take
# healthy refreshes in the same second down with them.
diff --git a/posthog/models/test/test_integration_model.py b/posthog/models/test/test_integration_model.py
index 20dbf8bba239..b9ea80c115d4 100644
--- a/posthog/models/test/test_integration_model.py
+++ b/posthog/models/test/test_integration_model.py
@@ -1125,6 +1125,30 @@ class TestOauthIntegrationModel(BaseTest):
),
("rate_limited", 429, {"status": "error", "errorType": "RATE_LIMIT"}, "hubspot", "rate_limited"),
("rate_limited_any_kind", 429, {}, None, "rate_limited"),
+ (
+ "meta_dead_token",
+ 400,
+ {
+ "error": {
+ "message": "Error validating access token: The session has been invalidated because the user changed their password.",
+ "type": "OAuthException",
+ "code": 190,
+ "error_subcode": 460,
+ }
+ },
+ "meta-ads",
+ "invalid_grant",
+ ),
+ (
+ "instagram_dead_token",
+ 400,
+ {"error": {"type": "OAuthException", "code": 190}},
+ "instagram",
+ "invalid_grant",
+ ),
+ ("meta_shape_on_other_kind", 400, {"error": {"code": 190}}, "hubspot", "other"),
+ ("meta_non_grant_error_code", 400, {"error": {"type": "OAuthException", "code": 10}}, "meta-ads", "other"),
+ ("meta_190_5xx_is_outage", 502, {"error": {"code": 190}}, "meta-ads", "http_5xx"),
]
)
def test_oauth_refresh_failure_reason(self, _name, status_code, body, kind, expected):