Kamal
Kamal

Reputation: 23

Cannot Access themes.json using shopify api and nodejs

I am not able to access the themes.json of my development store using shopify api and nodejs. Here is what I am doing:

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');
  }
});

There is this error showing that the access to {ngrok}.ngrok.io was denied while I can access product.json & shop.json with the help of same code

Upvotes: 0

Views: 163

Answers (1)

bknights
bknights

Reputation: 15412

Denied means your API key doesn’t have access. If this is a public app you need to add read_themes to your scopes. If it is a private app you need to go to the app setup and add theme access.

Upvotes: 1

Related Questions