Shopify GraphQL customerCreate突变需密码?求免密码创建客户方案
解决Shopify GraphQL免密码创建客户的问题
我需要在网站前端通过点击按钮调用Shopify的GraphQL突变创建客户,仅收集用户邮箱,提交后让用户收到账户激活邮件。根据Shopify官方文档,customerCreate突变无需密码,但API返回错误提示密码为必填项,求解决办法。
我的代码:
mutation customerCreate($input: CustomerCreateInput!) { customerCreate(input: $input) { customerUserErrors { code field message } customer { id } } }
变量:
{ "input": { "email": "example@gmail.com", "acceptsMarketing": true } }
错误信息:
"message": "类型为CustomerCreateInput!的变量$input的password字段提供了无效值(预期值不为null)"
解决办法:
问题出在两个可能的点,对应两种解决方式:
如果使用的是Shopify Admin API
在输入参数中添加sendEmailInvite: true,此时password字段可省略,Shopify会自动给用户发送账户激活邮件,引导用户设置密码。修正后的变量如下:{ "input": { "email": "example@gmail.com", "acceptsMarketing": true, "sendEmailInvite": true } }注意:Admin API不能直接在前端暴露调用,会泄露你的店铺管理员密钥,建议通过后端代理请求。
如果是在前端直接调用,必须使用Storefront API
Admin API不允许前端直接调用,你应该切换为Shopify Storefront API的customerCreate突变。该API专门面向前端,支持免密码创建客户,创建后用户会自动收到激活邮件。Storefront API的customerCreate突变写法和Admin API类似,但权限和端点不同,需要使用Storefront API密钥进行请求。
内容的提问来源于stack exchange,提问作者java-beginner-123
相关产品推荐
相关产品推荐

