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(google_sheets): name service account in permission-denied error (#72202)

Tom Owers2b5b4bbb66e0
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
index afde9a523cb9..656ec7d84e8a 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/source.py
@@ -113,7 +113,9 @@ class GoogleSheetsSource(SimpleSource[GoogleSheetsSourceConfig]):
except PermissionError:
return (
False,
- "Permissions missing from spreadsheet. View documentation at https://posthog.com/docs/cdp/sources/google-sheets",
+ "PostHog does not have access to this spreadsheet. Share it with our service account "
+ f"({settings.GOOGLE_SHEETS_SERVICE_ACCOUNT_CLIENT_EMAIL}) as a Viewer, then try again. "
+ "See https://posthog.com/docs/cdp/sources/google-sheets for more.",
)
except gspread.exceptions.APIError as e:
# gspread stringifies these as "APIError: [<code>]: <message>", which isn't actionable.
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
index 87fa4a078d49..90af863ceac8 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/google_sheets/tests/test_google_sheets.py
@@ -615,6 +615,20 @@ def test_validate_credentials_maps_api_error_to_friendly_message(api_message, ex
assert "APIError" not in (error_message or "")
+def test_validate_credentials_permission_denied_names_service_account(settings):
+ settings.GOOGLE_SHEETS_SERVICE_ACCOUNT_CLIENT_EMAIL = "[email protected]"
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.google_sheets.source.google_sheets_client"
+ ) as mock_client:
+ mock_client.return_value.open_by_url.side_effect = PermissionError()
+ config = GoogleSheetsSourceConfig(spreadsheet_url="https://docs.google.com/spreadsheets/d/fake")
+ is_valid, error_message = GoogleSheetsSource().validate_credentials(config, team_id=1)
+
+ assert is_valid is False
+ # The user needs the exact address to share the sheet with — not just a docs link.
+ assert "[email protected]" in (error_message or "")
+
+
@pytest.mark.parametrize(
"auth_error,expect_retry_hint",
[