无法使用shopify api和nodejs访问themes.json

时间:2019-06-14 04:33:46

标签: api shopify

我无法使用shopify api和nodejs访问开发商店的themes.json。 这是我在做什么:

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

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

  // Verifying Hmac
  if (shop && hmac && code) {
    const map = Object.assign({}, req.query);
    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 verification failed');
    }

    // Appending Access Token to the shop Url
    const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
    const accessTokenPayload = {
        client_id: apiKey,
        client_secret: apiSecret,
        code
    };

    // Making an API Request And getting an API response
    request.post(accessTokenRequestUrl, {json: accessTokenPayload })
    // Promise for Access Token Response
      .then((accessTokenResponse) => {
        const accessToken = accessTokenResponse.access_token;
        // Request URL for Products
        const apiRequestUrl = 'https://' + shop + '/admin/api/2019-04/themes.json'
        console.log(apiRequestUrl);
        const apiRequestHeader = {
            'X-Shopify-Access-Token': accessToken
        };

        request.get(apiRequestUrl, { headers: apiRequestHeader })
        .then((apiResponse) => {
            let example = JSON.parse(apiResponse);
            res.send(example);
          // End API Response
          res.end(apiResponse)
        }).catch((error) => {
          res.status(error.statusCode).send(error.error.error_descripton)
        });
    }).catch((error) => {
      res.status(error.statusCode).send(error.error.error_descripton)
    })
  } else {
    res.status(400).send('Required parameters missing');
  }
});

出现此错误,表明在我可以借助相同代码访问product.json和shop.json时,拒绝访问{ngrok} .ngrok.io

1 个答案:

答案 0 :(得分:1)

拒绝表示您的API密钥没有访问权限。如果这是一个公共应用,则需要在范围内添加read_themes。如果它是私人应用程序,则需要转到应用程序设置并添加主题访问权限。