Shopify 顾客账户

Customer Account API 授权回调 400 怎么排查

现象#

自建站在回调里换 token 时收到 400 invalid_grant,或者授权请求直接被弹回 redirect_uri_mismatch,服务端日志只有一行 invalid_request。

原因#

  • redirect_uri 必须与 Customer Account API 设置里登记的值逐字符一致,多一个结尾斜杠就不通过
  • 公开客户端没带 PKCE,缺 code_challenge 与 S256,或者换 token 时用了另一份 verifier
  • 回调指向 http://localhost,Customer Account API 只接受 HTTPS 的公网地址,本地必须走隧道
  • 授权端点写死在代码里,没从 openid-configuration 发现,换账户子域后所有地址一起失效

处理步骤#

  1. 请求 /.well-known/openid-configuration,取返回里的 authorization_endpoint 与 token_endpoint
  2. 在 Partner 后台的 Customer Account API 设置里登记回调 https://app.example.com/api/auth/callback
  3. 授权请求带 scope=openid email customer-account-api:full 与 response_type=code
  4. 把 code_verifier 存进 session,challenge 用 SHA256 生成,回调先校验 state 再换 token
  5. 换 token 的 body 用 grant_type=authorization_code,verifier 必须与授权时是同一份

验收标准#

用测试顾客完整走一遍,拿到 access_token 后查 customer { firstName } 有值;把同一个 code 再提交一次应返回 invalid_grant。

本文由 风格技术 技术团队整理,适用于 Shopify 标准版与 Plus 店铺。Shopify 平台能力迭代较快,具体操作路径与字段以官方文档和店铺后台实际版本为准。