首页 文章

将Polaris与Shopify App连接

提问于
浏览
0

我是shopify-App developmet的新手,遇到了Polaris,Shopify提供的库可以实现一致的用户界面开发 . 我的想法是构建一个Node.js Express应用程序来验证和安装应用程序并处理数据,并为商店管理员的用户界面提供反应应用程序 .

我在网上搜索,但找不到将react应用程序连接到shopify应用程序的标准或推荐方法 .

我可以想象的是当商店管理员从应用程序部分选择应用程序并成功通过身份验证后,从Node应用程序重定向到React应用程序 .

app.get('/shopify/callback', (req, res) => {
  const { shop, hmac, code, state } = req.query;
  const stateCookie = cookie.parse(req.headers.cookie).state;

  if (state !== stateCookie) {
    return res.status(403).send('Request origin cannot be verified');
  }

  if (shop && hmac && code) {
    const map = Object.assign({}, req.query);
    delete map['signature'];
    delete map['hmac'];
    const message = querystring.stringify(map);
    const generatedHash = crypto
      .createHmac('sha256', apiSecret)
      .update(message)
      .digest('hex');

    if (generatedHash !== hmac) {
      return res.status(400).send('HMAC validation failed');
    }

    const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
    const accessTokenPayload = {
      client_id: apiKey,
      client_secret: apiSecret,
      code,
    };

    request.post(accessTokenRequestUrl, { json: accessTokenPayload })
      .then((accessTokenResponse) => {
        const accessToken = accessTokenResponse.access_token;

        const shopRequestUrl = 'https://' + shop + '/admin/shop.json';
        const shopRequestHeaders = {
          'X-Shopify-Access-Token': accessToken,
        };
        res.redirect([URL to the react app built with Polaris]);
      })
      .catch((error) => {
        res.status(error.statusCode).send(error.error.error_description);
      });

  } else {
    res.status(400).send('Required parameters missing');
  }
});

我有这个工作,但我不确定这是否是推荐的方法 .

1 回答

  • 1

    它们有3种不同的东西:Node.JS,React和Polaris,您可以选择其中任何一种 . Polaris是一个库,如果你想使用它,只需运行 yarn add @shopify-polaris 并阅读其文档 . 就这样 .

    无论有没有Polaris,您仍然可以使用各种堆栈创建Shopify应用程序 .

相关问题