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): classify reddit dead-grant refresh failures as invalid_grant (#70985)

Harley Alexanderdbf4d853be71
diff --git a/posthog/models/integration.py b/posthog/models/integration.py
index e09f0ae2f49f..d4d7bb61f551 100644
--- a/posthog/models/integration.py
+++ b/posthog/models/integration.py
@@ -114,12 +114,17 @@ REFRESH_FAILURE_REASON_HTTP_5XX = "http_5xx"
REFRESH_FAILURE_REASON_OTHER = "other"
-def oauth_refresh_failure_reason(status_code: int, body: dict) -> str:
+def oauth_refresh_failure_reason(status_code: int, body: dict, kind: str | None = None) -> str:
error = body.get("error")
if error == REFRESH_FAILURE_REASON_INVALID_GRANT:
return REFRESH_FAILURE_REASON_INVALID_GRANT
if error == REFRESH_FAILURE_REASON_INVALID_CLIENT:
return REFRESH_FAILURE_REASON_INVALID_CLIENT
+ # Reddit reports a dead grant as `{"message": "Bad Request", "error": 400}` with no OAuth
+ # error code. Our refresh request shape is fixed and succeeds fleet-wide, so a 400 on this
+ # endpoint means the grant, not the request - match that exact shape for reddit only.
+ if kind == "reddit-ads" and status_code == 400 and error == 400:
+ return REFRESH_FAILURE_REASON_INVALID_GRANT
if status_code >= 500:
return REFRESH_FAILURE_REASON_HTTP_5XX
return REFRESH_FAILURE_REASON_OTHER
@@ -1357,7 +1362,7 @@ class OauthIntegration:
if res.status_code != 200 or not config.get("access_token"):
logger.warning(f"Failed to refresh token for {self}", response=res.text)
self.integration.errors = ERROR_TOKEN_REFRESH_FAILED
- reason = oauth_refresh_failure_reason(res.status_code, config)
+ reason = oauth_refresh_failure_reason(res.status_code, config, kind=self.integration.kind)
attempt = record_refresh_failure(self.integration, reason=reason)
oauth_refresh_counter.labels(
kind=self.integration.kind, result="failed", reason=reason, attempt=attempt
@@ -3281,7 +3286,7 @@ class MetaAdsIntegration:
if res.status_code != 200 or not config.get("access_token"):
logger.warning(f"Failed to refresh token for {self}", response=res.text)
self.integration.errors = ERROR_TOKEN_REFRESH_FAILED
- reason = oauth_refresh_failure_reason(res.status_code, config)
+ reason = oauth_refresh_failure_reason(res.status_code, config, kind=self.integration.kind)
attempt = record_refresh_failure(self.integration, reason=reason)
oauth_refresh_counter.labels(
kind=self.integration.kind, result="failed", reason=reason, attempt=attempt
diff --git a/posthog/models/test/test_integration_model.py b/posthog/models/test/test_integration_model.py
index a24720986703..a96a6109510e 100644
--- a/posthog/models/test/test_integration_model.py
+++ b/posthog/models/test/test_integration_model.py
@@ -770,15 +770,19 @@ class TestOauthIntegrationModel(BaseTest):
@parameterized.expand(
[
- ("invalid_grant", 400, {"error": "invalid_grant"}, "invalid_grant"),
- ("invalid_client", 401, {"error": "invalid_client"}, "invalid_client"),
- ("server_error", 502, {}, "http_5xx"),
- ("other_4xx", 400, {"error": "temporarily_unavailable"}, "other"),
- ("non_string_error", 400, {"error": {"code": 1}}, "other"),
+ ("invalid_grant", 400, {"error": "invalid_grant"}, None, "invalid_grant"),
+ ("invalid_client", 401, {"error": "invalid_client"}, None, "invalid_client"),
+ ("server_error", 502, {}, None, "http_5xx"),
+ ("other_4xx", 400, {"error": "temporarily_unavailable"}, None, "other"),
+ ("non_string_error", 400, {"error": {"code": 1}}, None, "other"),
+ ("reddit_dead_grant_shape", 400, {"message": "Bad Request", "error": 400}, "reddit-ads", "invalid_grant"),
+ ("reddit_shape_on_other_kind", 400, {"message": "Bad Request", "error": 400}, "hubspot", "other"),
+ ("reddit_5xx", 502, {"message": "Bad Gateway", "error": 502}, "reddit-ads", "http_5xx"),
+ ("reddit_oauth_error_code", 400, {"error": "invalid_grant"}, "reddit-ads", "invalid_grant"),
]
)
- def test_oauth_refresh_failure_reason(self, _name, status_code, body, expected):
- assert oauth_refresh_failure_reason(status_code, body) == expected
+ def test_oauth_refresh_failure_reason(self, _name, status_code, body, kind, expected):
+ assert oauth_refresh_failure_reason(status_code, body, kind=kind) == expected
@patch("posthog.models.integration.requests.post")
def test_reconnect_clears_backoff_state(self, mock_post):