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): explain oauth failures caused by rejected app credentials (#125504)

Tom Owers53a8a2c15dfa
diff --git a/posthog/models/integration/oauth.py b/posthog/models/integration/oauth.py
index 117714c49d4e..976b4a0ec564 100644
--- a/posthog/models/integration/oauth.py
+++ b/posthog/models/integration/oauth.py
@@ -50,6 +50,11 @@ def _extract_oauth_error_message(res: requests.Response) -> str | None:
return text[:300] if text else None
if isinstance(body, dict):
+ graph_error = body.get("error")
+ if isinstance(graph_error, dict):
+ # Meta's Graph API nests the error in an object whose trace id and codes mean nothing to the customer.
+ message = graph_error.get("message")
+ return str(message) if message else None
description = body.get("error_description") or body.get("message")
code = body.get("error")
if description and code:
@@ -78,6 +83,12 @@ _STRIPE_MODE_MISMATCH_MESSAGE = (
)
+_CLIENT_CREDENTIALS_REJECTED_MESSAGE = (
+ "PostHog couldn't finish this connection because the provider rejected PostHog's own app credentials. "
+ "Contact support so we can fix it."
+)
+
+
def _oauth_error_field(res: requests.Response, field: str) -> str | None:
try:
body = res.json()
@@ -91,6 +102,19 @@ def _oauth_error_code(res: requests.Response) -> str | None:
return _oauth_error_field(res, "error")
+def _is_client_credentials_rejected(res: requests.Response) -> bool:
+ # The provider refused PostHog's client id or secret, so starting the connection again fails the same way.
+ if _oauth_error_code(res) == "invalid_client":
+ return True
+ try:
+ body = res.json()
+ except Exception:
+ return False
+ graph_error = body.get("error") if isinstance(body, dict) else None
+ message = graph_error.get("message") if isinstance(graph_error, dict) else None
+ return isinstance(message, str) and "validating client secret" in message.lower()
+
+
def _is_stripe_mode_mismatch(kind: str, res: requests.Response) -> bool:
# The install link that issued the code fixes its mode, and Stripe redeems it only with the secret
# key of that mode. A retry gets a code from the same link, so it fails the same way.
@@ -113,6 +137,8 @@ def _raise_oauth_validation_error(kind: str, res: requests.Response) -> NoReturn
# The code expired, was already exchanged, or was issued for another account. The
# provider's description can echo the code itself, so it stays in the logs.
raise ValidationError(_INVALID_GRANT_MESSAGE)
+ if _is_client_credentials_rejected(res):
+ raise ValidationError(_CLIENT_CREDENTIALS_REJECTED_MESSAGE)
provider_error = _extract_oauth_error_message(res)
if provider_error:
raise ValidationError(f"{kind} OAuth failed: {provider_error}")
diff --git a/posthog/models/test/integration/test_oauth.py b/posthog/models/test/integration/test_oauth.py
index 81de778adcd6..51245fd310ce 100644
--- a/posthog/models/test/integration/test_oauth.py
+++ b/posthog/models/test/integration/test_oauth.py
@@ -78,6 +78,8 @@ class TestOauthIntegrationModel(BaseTest):
"HELPSCOUT_APP_CLIENT_SECRET": "helpscout-client-secret",
"STRIPE_APP_CLIENT_ID": "stripe-client-id",
"STRIPE_APP_SECRET_KEY": "stripe-secret-key",
+ "INSTAGRAM_APP_CLIENT_ID": "instagram-client-id",
+ "INSTAGRAM_APP_CLIENT_SECRET": "instagram-client-secret",
}
def create_integration(
@@ -277,11 +279,55 @@ class TestOauthIntegrationModel(BaseTest):
"json_error_body_other_code",
"salesforce",
400,
+ {"error": "invalid_request", "error_description": "Missing redirect_uri."},
+ None,
+ '{"error":"invalid_request","error_description":"Missing redirect_uri."}',
+ ["invalid_request", "Missing redirect_uri"],
+ [],
+ ),
+ (
+ "client_credentials_rejected",
+ "salesforce",
+ 401,
{"error": "invalid_client", "error_description": "Client authentication failed."},
None,
'{"error":"invalid_client","error_description":"Client authentication failed."}',
- ["invalid_client", "Client authentication failed"],
- [],
+ ["Contact support"],
+ ["invalid_client", "Start the connection again"],
+ ),
+ (
+ "meta_client_secret_rejected",
+ "instagram",
+ 400,
+ {
+ "error": {
+ "message": "Error validating client secret.",
+ "type": "OAuthException",
+ "code": 1,
+ "fbtrace_id": "fake-trace-id",
+ }
+ },
+ None,
+ '{"error":{"message":"Error validating client secret.","type":"OAuthException","code":1,"fbtrace_id":"fake-trace-id"}}',
+ ["Contact support"],
+ ["OAuthException", "fake-trace-id"],
+ ),
+ (
+ "meta_nested_error_object",
+ "instagram",
+ 400,
+ {
+ "error": {
+ "message": "Invalid verification code format.",
+ "type": "OAuthException",
+ "code": 100,
+ "fbtrace_id": "fake-trace-id",
+ }
+ },
+ None,
+ '{"error":{"message":"Invalid verification code format.","type":"OAuthException","code":100,"fbtrace_id":"fake-trace-id"}}',
+ ["Invalid verification code format"],
+ ["OAuthException", "fake-trace-id", "Contact support"],
),
(
"non_json_error_body",