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): ask Shopify for JSON oauth token errors (#86474)

estefania87ed752cb5fd
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
index 548641d58a33..a31215ae23f7 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/shopify.py
@@ -68,6 +68,16 @@ SHOPIFY_ACCESS_TOKEN_UNSUPPORTED_GRANT_ERROR = (
"docs, then enter its client ID and secret."
)
+# Raised on a 4xx whose body reports `error: shop_not_permitted`. Shopify only allows the
+# client_credentials grant when the app and the store belong to the same Shopify organization,
+# so re-entering credentials can never fix it. Surfaced separately so the message points at the
+# organization rather than the credentials.
+SHOPIFY_ACCESS_TOKEN_SHOP_NOT_PERMITTED_ERROR = (
+ "Shopify doesn't allow this app to connect to this store (shop_not_permitted). The app and "
+ "the store must be in the same Shopify organization. In the Shopify Dev Dashboard, open the "
+ "organization that contains your store and create the app there."
+)
+
# Raised when the OAuth token endpoint returns 404 — there is no store at
# `<store-id>.myshopify.com`, so the store id is wrong or the store no longer exists.
# Reconnecting the app can't fix a bad store id, so this is surfaced separately from the
@@ -377,6 +387,8 @@ def _access_token_auth_error_message(error_code: str | None) -> str:
return SHOPIFY_ACCESS_TOKEN_INVALID_CLIENT_ERROR
if error_code == "unsupported_grant_type":
return SHOPIFY_ACCESS_TOKEN_UNSUPPORTED_GRANT_ERROR
+ if error_code == "shop_not_permitted":
+ return SHOPIFY_ACCESS_TOKEN_SHOP_NOT_PERMITTED_ERROR
return SHOPIFY_ACCESS_TOKEN_AUTH_ERROR
@@ -419,7 +431,9 @@ def _get_shopify_access_token(shopify_store_id: str, shopify_client_id: str, sho
"client_secret": shopify_client_secret,
"grant_type": SHOPIFY_ACCESS_TOKEN_GRANT,
}
- access_res = make_tracked_session().post(access_token_url, data=access_data)
+ # The Accept header is load-bearing: without it Shopify renders 4xx OAuth errors as an HTML
+ # page instead of JSON, which leaves `_parse_oauth_error` with no error code to read.
+ access_res = make_tracked_session(headers={"Accept": "application/json"}).post(access_token_url, data=access_data)
if not access_res.ok:
# A 404 means there's no store at this subdomain — the store id is wrong or the store
# is gone. Reconnecting the app can't fix that, so point the user at the store id
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/source.py b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/source.py
index 8c22da93435c..535a50f60de2 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/source.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/source.py
@@ -29,6 +29,7 @@ from products.warehouse_sources.backend.temporal.data_imports.sources.shopify.se
from products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify import (
SHOPIFY_ACCESS_TOKEN_AUTH_ERROR,
SHOPIFY_ACCESS_TOKEN_INVALID_CLIENT_ERROR,
+ SHOPIFY_ACCESS_TOKEN_SHOP_NOT_PERMITTED_ERROR,
SHOPIFY_ACCESS_TOKEN_UNSUPPORTED_GRANT_ERROR,
SHOPIFY_GRAPHQL_ACCESS_DENIED_ERROR,
SHOPIFY_GRAPHQL_UNAUTHORIZED_ERROR_MATCH,
@@ -75,6 +76,9 @@ class ShopifySource(ResumableSource[ShopifySourceConfig, ShopifyResumeConfig]):
# 4xx `unsupported_grant_type` — the app can't use the client_credentials grant, so
# the user needs a Dev Dashboard app instead of a legacy custom app.
SHOPIFY_ACCESS_TOKEN_UNSUPPORTED_GRANT_ERROR: SHOPIFY_ACCESS_TOKEN_UNSUPPORTED_GRANT_ERROR,
+ # 4xx `shop_not_permitted`: the store is not in the app's Shopify organization, so
+ # minting a token fails regardless of the credentials entered.
+ SHOPIFY_ACCESS_TOKEN_SHOP_NOT_PERMITTED_ERROR: SHOPIFY_ACCESS_TOKEN_SHOP_NOT_PERMITTED_ERROR,
# 404 from the same endpoint — no store at this subdomain. Retrying cannot
# recover; the user must correct the store id.
SHOPIFY_STORE_NOT_FOUND_ERROR: SHOPIFY_STORE_NOT_FOUND_ERROR,
diff --git a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py
index 9b3ed4106113..6ce5433b1806 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/sources/shopify/tests/test_access_token.py
@@ -6,6 +6,7 @@ from requests.exceptions import ChunkedEncodingError, SSLError
from products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify import (
SHOPIFY_ACCESS_TOKEN_AUTH_ERROR,
SHOPIFY_ACCESS_TOKEN_INVALID_CLIENT_ERROR,
+ SHOPIFY_ACCESS_TOKEN_SHOP_NOT_PERMITTED_ERROR,
SHOPIFY_ACCESS_TOKEN_UNSUPPORTED_GRANT_ERROR,
SHOPIFY_STORE_NOT_FOUND_ERROR,
ShopifyRetryableError,
@@ -53,6 +54,7 @@ def test_get_access_token_4xx_is_non_retryable(status_code):
[
("invalid_client", SHOPIFY_ACCESS_TOKEN_INVALID_CLIENT_ERROR),
("unsupported_grant_type", SHOPIFY_ACCESS_TOKEN_UNSUPPORTED_GRANT_ERROR),
+ ("shop_not_permitted", SHOPIFY_ACCESS_TOKEN_SHOP_NOT_PERMITTED_ERROR),
],
)
def test_get_access_token_4xx_maps_shopify_error_code(error_code, expected_message):
@@ -73,6 +75,37 @@ def test_get_access_token_4xx_maps_shopify_error_code(error_code, expected_messa
)
+def test_get_access_token_asks_shopify_for_json_errors():
+ # Without an explicit JSON Accept header Shopify renders 4xx OAuth errors as an HTML page,
+ # so _parse_oauth_error never sees the error code and every failure collapses to the
+ # generic message.
+ session_factory = mock.MagicMock(
+ return_value=mock.MagicMock(
+ post=mock.MagicMock(return_value=_mock_response(200, ok=True, json_data={"access_token": "tok"}))
+ )
+ )
+ with mock.patch(
+ "products.warehouse_sources.backend.temporal.data_imports.sources.shopify.shopify.make_tracked_session",
+ session_factory,
+ ):
+ _get_shopify_access_token("store", "client-id", "client-secret")
+ assert session_factory.call_args.kwargs["headers"] == {"Accept": "application/json"}
+
+
+def test_get_access_token_4xx_html_body_falls_back_to_generic_message():
+ # Shopify (or an intermediary) can still answer a 4xx with a non-JSON body; the parse must
+ # degrade to the generic credentials message instead of raising a decode error.
+ response = _mock_response(400, ok=False)
+ response.json.side_effect = ValueError("Expecting value: line 1 column 1 (char 0)")
+ with _patched_token_call(response):
+ with pytest.raises(Exception) as exc_info:
+ _get_shopify_access_token("store", "client-id", "client-secret")
+
+ error_message = str(exc_info.value)
+ assert SHOPIFY_ACCESS_TOKEN_AUTH_ERROR in error_message
+ assert "HTTP 400" in error_message
+
+
def test_get_access_token_404_reports_missing_store():
# A 404 means the store subdomain doesn't resolve to a real store — a distinct,
# non-retryable failure that reconnecting the app can't fix, so it must surface the