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(ws): handle expired Stripe OAuth tokens with refresh and clear error messages (#57689)

estefania90f6b721c6e9
diff --git a/posthog/models/integration.py b/posthog/models/integration.py
index ec4a11f64ae4..b1bbaa7de5da 100644
--- a/posthog/models/integration.py
+++ b/posthog/models/integration.py
@@ -957,6 +957,10 @@ class OauthIntegration:
# Default to 1 hour for Salesforce if not provided (conservative)
config["expires_in"] = 3600
+ # Stripe Apps OAuth tokens don't include expires_in in the response
+ if not config.get("expires_in") and kind == "stripe":
+ config["expires_in"] = 3600
+
if kind == "stripe":
# Persisted so downstream Stripe API calls (refresh_access_token,
# StripeIntegration.write_posthog_secrets / clear_posthog_secrets)
@@ -996,6 +1000,9 @@ class OauthIntegration:
# Salesforce tokens typically last 2-4 hours, we'll assume 1 hour (3600 seconds) to be conservative
expires_in = 3600
+ if not expires_in and self.integration.kind == "stripe":
+ expires_in = 3600
+
if not expires_in or not refreshed_at:
return False
@@ -1097,10 +1104,11 @@ class OauthIntegration:
if config.get("refresh_token"):
self.integration.sensitive_config["refresh_token"] = config["refresh_token"]
- # Handle case where Salesforce doesn't provide expires_in in refresh response
+ # Handle case where Salesforce/Stripe doesn't provide expires_in in refresh response
expires_in = config.get("expires_in")
if not expires_in and self.integration.kind == "salesforce":
- # Default to 1 hour for Salesforce if not provided (conservative)
+ expires_in = 3600
+ if not expires_in and self.integration.kind == "stripe":
expires_in = 3600
self.integration.config["expires_in"] = expires_in
diff --git a/posthog/temporal/data_imports/sources/stripe/source.py b/posthog/temporal/data_imports/sources/stripe/source.py
index b62c81aba6b3..a17d62834096 100644
--- a/posthog/temporal/data_imports/sources/stripe/source.py
+++ b/posthog/temporal/data_imports/sources/stripe/source.py
@@ -19,6 +19,7 @@ from posthog.schema import (
SuggestedTable,
)
+from posthog.models.integration import OauthIntegration
from posthog.temporal.data_imports.pipelines.pipeline.typings import SourceInputs, SourceResponse
from posthog.temporal.data_imports.sources.common.base import (
ExternalWebhookInfo,
@@ -238,6 +239,7 @@ If automatic creation failed due to a permissions error and you're using a restr
"Missing integration ID": "Integration ID is not configured. Please reconnect your Stripe account.",
"Integration not found": "The linked Stripe integration no longer exists. Please reconnect your Stripe account.",
"Stripe access token not found": "Stripe OAuth access token is missing. Please reconnect your Stripe account.",
+ "Your Stripe OAuth connection has expired or been revoked. Please reconnect your Stripe account.": "Your Stripe OAuth connection has expired or been revoked. Please reconnect your Stripe account.",
}
def _get_api_key(self, config: StripeSourceConfig, team_id: int) -> str:
@@ -250,6 +252,11 @@ If automatic creation failed due to a permissions error and you're using a restr
raise ValueError("Missing Stripe integration ID")
integration = self.get_oauth_integration(config.auth_method.stripe_integration_id, team_id)
+
+ oauth_integration = OauthIntegration(integration)
+ if oauth_integration.access_token_expired():
+ oauth_integration.refresh_access_token()
+
if not integration.access_token:
raise ValueError("Stripe access token not found")
return integration.access_token
@@ -291,6 +298,11 @@ If automatic creation failed due to a permissions error and you're using a restr
else:
return False, "Invalid Stripe credentials"
except StripeAuthenticationError as e:
+ if config.auth_method.selection == "oauth":
+ return (
+ False,
+ "Your Stripe OAuth connection has expired or been revoked. Please reconnect your Stripe account.",
+ )
return (
False,
f"Stripe rejected the API key: {e.stripe_message}. Double-check that you pasted a restricted key (rk_live_...) for the same Stripe account, with no extra whitespace, and that it has not been revoked.",