接着上次的项目,我们这次把项目安装到 Shopify 商店。
首先,我们要有个开发者账号,来这里 注册。
创建公共应用
我们创建一个公共应用,这时,我们会得到 client_id
client_secret
,然后设置应用的 url 以及 重定向 url
有条件的可以进行穿透 ,也可以填写本地地址 (https://127.0.0.1:3000/)
OAuth 身份验证
回到项目中 ,创建路由 安装路由 (/install) 和认证路由 (/auth/callback)
/install
- 获得安装商店 xxx.myshopify.com
- 该应用需要的权限
- 该应用的 client_id
- 重定向 URl
- 设置一个随机数
- 重定向到
1 | `https://${shop}/admin/oauth/authorize?client_id=${client_id}&scope=${scopes}&redirect_uri=${HOST}/auth/callback&state=${nonce}`; |
/auth/callback
- 获取参数 code, hmac, shop, state, timestamp
- 判断 state 与我们设置的随机数一样并且验证 hmac
- 通过 code 获取访问令牌
1 | let res = await fetch(`https://${shop}/admin/oauth/access_token`, { |
- 保存 access_token
- 设置 token (JWT 认证)
- 进入应用(重定向)
编写应用功能代码
…….