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(warehouse-sources): say how to fix a rejected Calendly token (#100696)

Tom Owers4e899ffd4aff
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/calendly.py b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/calendly.py
index edf9327f6718..53d7c7670a50 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/calendly.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/calendly.py
@@ -93,13 +93,17 @@ def _get_headers(token: str) -> dict[str, str]:
}
-def validate_credentials(token: str) -> bool:
- ok, _status = validate_via_probe(
+def validate_credentials(token: str) -> tuple[bool, int | None]:
+ """Probe `/users/me` and report `(is_valid, status_code)`.
+
+ The caller needs the status to tell a rejected token (401) from one Calendly accepts but
+ scopes too narrowly (403), which are two different fixes for the user.
+ """
+ return validate_via_probe(
lambda: make_tracked_session(redact_values=(token,)),
f"{CALENDLY_BASE_URL}/users/me",
headers=_get_headers(token),
)
- return ok
def get_current_organization(token: str, base_url: str = CALENDLY_BASE_URL) -> str:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/source.py
index 8f7ead0a091b..f0b43dd292ec 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/source.py
@@ -169,10 +169,26 @@ Paste the same signing key into the field below so PostHog can verify deliveries
schema_name: Optional[str] = None,
api_version: str | None = None,
) -> tuple[bool, str | None]:
- if validate_calendly_credentials(config.personal_access_token):
+ is_valid, status = validate_calendly_credentials(config.personal_access_token)
+ if is_valid:
return True, None
- return False, "Invalid Calendly personal access token"
+ if status == 401:
+ return (
+ False,
+ "Calendly rejected your personal access token. Create a new token under "
+ "Integrations → API & Webhooks, then reconnect.",
+ )
+ # The probe reads `/users/me`, which every token may call, so a 403 is an account-wide
+ # restriction rather than a table the token was not scoped for.
+ if status == 403:
+ return (
+ False,
+ "Your Calendly personal access token does not have the required permissions. "
+ "Create a token with access to your Calendly organization, then reconnect.",
+ )
+
+ return False, "PostHog couldn't reach Calendly to check your token. Try connecting again in a few minutes."
def get_non_retryable_errors(self) -> dict[str, str | None]:
return {
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly.py b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly.py
index 9802364379eb..198cdef2de2c 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly.py
@@ -132,11 +132,11 @@ class TestValidateCredentials:
self, _name: str, status_code: int, expected: bool, mock_session: mock.MagicMock
) -> None:
mock_session.return_value.get.return_value = mock.MagicMock(status_code=status_code)
- assert validate_credentials("token") is expected
+ assert validate_credentials("token") == (expected, status_code)
@mock.patch(CALENDLY_SESSION_PATCH, side_effect=Exception("network down"))
def test_validate_credentials_swallows_exceptions(self, _mock_session: mock.MagicMock) -> None:
- assert validate_credentials("token") is False
+ assert validate_credentials("token") == (False, None)
class TestGetCurrentOrganization:
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly_source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly_source.py
index 62df7ac8d9ac..849b0d0eb653 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly_source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/calendly/tests/test_calendly_source.py
@@ -86,22 +86,28 @@ class TestCalendlySource:
assert by_name[name].incremental_fields == []
@pytest.mark.parametrize(
- "mock_return, expected_valid, expected_message",
+ "mock_return, expected_valid, expected_message_fragment",
[
- (True, True, None),
- (False, False, "Invalid Calendly personal access token"),
+ ((True, 200), True, None),
+ ((False, 401), False, "Create a new token"),
+ ((False, 403), False, "required permissions"),
+ ((False, None), False, "couldn't reach Calendly"),
],
)
@mock.patch(
"products.warehouse_sources.backend.temporal.data_imports.sources.calendly.source.validate_calendly_credentials"
)
- def test_validate_credentials(self, mock_validate, mock_return, expected_valid, expected_message):
+ def test_validate_credentials(self, mock_validate, mock_return, expected_valid, expected_message_fragment):
mock_validate.return_value = mock_return
is_valid, error_message = self.source.validate_credentials(self.config, self.team_id)
assert is_valid is expected_valid
- assert error_message == expected_message
+ if expected_message_fragment is None:
+ assert error_message is None
+ else:
+ assert error_message is not None
+ assert expected_message_fragment in error_message
mock_validate.assert_called_once_with(self.config.personal_access_token)
@mock.patch("products.warehouse_sources.backend.temporal.data_imports.sources.calendly.source.calendly_source")