无法验证请求来源 - Shopify

Request origin cannot be verified - Shopify

我正在为 Shopify 开发一个应用程序。目前处于开发阶段。到目前为止,我已经成功地授权了该应用程序,然后使用嵌入式应用程序 SDK 将其重定向回管理页面。但是,当我 return 到管理页面时,它给我一个错误提示 Request origin cannot be verified.

控制台显示Failed to load resource: the server responded with a status of 403 (Forbidden) 控制台中的 URL 是这样的 https://myshop.myshopify.com/admin/apps/dfdjf4343343434343434bfdf/shopify/shopify/callback?code=ffdfdffd&hmac=fdfdfdfdfdfdfdfdfddfdfdfdfdf&shop=myshop.myshopify.com&state=151193864548800&timestamp=1511938648

fdfdfdfdfdfdfdfdfddfdfdfdfdf 只是我替换的随机字符,而不是散列。仅供参考 - 我已从图像中删除了应用程序名称和用户个人资料名称以及头像。

发生这种情况是因为您无法匹配在 cookie 中设置的状态,同时响应重定向 url

const ShopifyToken = require('shopify-token')

const forwardingAddress = process.env.HOST

const shopifyToken = new ShopifyToken({
  sharedSecret: process.env.SHOPIFY_API_SECRET,
  redirectUri: forwardingAddress + '/shopify/callback',
  apiKey: process.env.SHOPIFY_API_KEY
})


const shopify = {
  // use this for authentication
  auth: (req, res, next) => {
    const shop = req.query.shop
    if (!shop) {
      return res.status(400).send('Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request')
    }
    const shopRegex = /^([\w-]+)\.myshopify\.com/i
    const shopName = shopRegex.exec(shop)[1]
    const state = shopifyToken.generateNonce()
    const url = shopifyToken.generateAuthUrl(shopName, scopes, state)
    res.cookie('state', state)
    res.redirect(url)
  },

  // use this as your callback function
  authCallback: async (req, res) => {
    const { shop, hmac, code, state } = req.query
    const stateCookie = cookie.parse(req.headers.cookie).state
    if (state !== stateCookie) {
    // you are unable to set proper state ("nonce") in this case, thus you are getting this error
      return res.status(403).send('Request origin cannot be verified')
    }
    if (!shop || !hmac || !code) {
      res.status(400).send('Required parameters missing')
    }
    let hmacVerified = shopifyToken.verifyHmac(req.query)
    console.log(`verifying -> ${hmacVerified}`)
    // DONE: Validate request is from Shopify
    if (!hmacVerified) {
      return res.status(400).send('HMAC validation failed')
    }
    const accessToken = await shopifyToken.getAccessToken(shop, code)
    const shopRequestUrl = 'https://' + shop + '/admin/shop.json'
    const shopRequestHeaders = {
      'X-Shopify-Access-Token': accessToken
    }
    try {
      const shopResponse = await request.get(shopRequestUrl, { headers: shopRequestHeaders })
      res.status(200).end(shopResponse)
    } catch (error) {
      res.status(error.statusCode).send(error.error.error_description)
    }
  }
}

const express = require('express');
const router = express.Router();
const dotenv = require('dotenv').config();
const cookie = require('cookie');
const requestPromise = require('request-promise');
const ShopifyToken = require('shopify-token');

const scopes = "write_products";
const forwardingAddress = process.env.HOST;

var shopifyToken = new ShopifyToken({
sharedSecret: process.env.SHOPIFY_API_SECRET,
redirectUri: forwardingAddress + '/shopify/callback',
apiKey: process.env.SHOPIFY_API_KEY
})

router.get('/shopify', (req, res) => {
const shop = req.query.shop;
if (!shop) {
    return res.status(400).send('Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request')
}
const shopRegex = /^([\w-]+)\.myshopify\.com/i
const shopName = shopRegex.exec(shop)[1]
const state = shopifyToken.generateNonce();
const url = shopifyToken.generateAuthUrl(shopName, scopes, state);
res.cookie('state', state);
res.redirect(url);
});

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

if (state !== stateCookie) {
    // you are unable to set proper state ("nonce") in this case, thus you are getting this error
    return res.status(403).send('Request origin cannot be verified')
}
if (!shop || !hmac || !code) {
    res.status(400).send('Required parameters missing')
}
let hmacVerified = shopifyToken.verifyHmac(req.query)
console.log(`verifying -> ${hmacVerified}`)

// DONE: Validate request is from Shopify
if (!hmacVerified) {
    return res.status(400).send('HMAC validation failed')
}
const accessToken = shopifyToken.getAccessToken(shop, code);
const shopRequestUrl = 'https://' + shop + '/admin/products.json'
const shopRequestHeaders = {
    'X-Shopify-Access-Token': accessToken
}
try {
    const shopResponse = requestPromise.get(shopRequestUrl, { headers: shopRequestHeaders })
    res.status(200).send(shopResponse)
} catch (error) {
    res.status(error.statusCode).send(error.error.error_description)
}

});

module.exports = router;

虽然这很简单,但还要确保协议与您输入的内容相匹配以启动应用程序安装。

如果您不小心将 http 用于 http://you.ngrok.io/,但您的回调重定向到 https(即 https://you.ngrok.io/auth/callback),OAuth 握手将失败。