我在这里查看了其他 shopify 问题,但这次错误似乎来自我认为的其他问题。我正在尝试通过 nodejs 上的 localhost 创建/访问 shopify 应用程序,以便将来使用产品 API。但是遇到上面的错误:
这是我的 NGROK 日志:
GET /shopify 302 Found GET /shopify 302 Found GET /shopify 302 Found GET /shopify 302 Found GET /shopify 302 Found GET /shopify 302 Found
1-这是我的 index.js 文件:
///////////// Initial Setup /////////////
const dotenv = require('dotenv').config();
const express = require('express');
const crypto = require('crypto');
const cookie = require('cookie');
const nonce = require('nonce')();
const querystring = require('querystring');
const axios = require('axios');
const shopifyApiPublicKey = process.env.SHOPIFY_API_PUBLIC_KEY;
const shopifyApiSecretKey = process.env.SHOPIFY_API_SECRET_KEY;
const scopes = 'write_products';
const appUrl = 'https://20a11edc124f.ngrok.io/';
const app = express();
const PORT = 3000
app.get('/', (req, res) => {
res.send('Ello Govna')
});
///////////// Helper Functions /////////////
const buildRedirectUri = () => `${appUrl}/shopify/callback`;
const buildInstallUrl = (shop, state, redirectUri) => `https://${shop}/admin/oauth/authorize?client_id=${shopifyApiPublicKey}&scope=${scopes}&state=${state}&redirect_uri=${redirectUri}`;
const buildAccessTokenRequestUrl = (shop) => `https://${shop}/admin/oauth/access_token`;
const buildShopDataRequestUrl = (shop) => `https://${shop}/admin/shop.json`;
const generateEncryptedHash = (params) => crypto.createHmac('sha256', shopifyApiSecretKey).update(params).digest('hex');
const fetchAccessToken = async (shop, data) => await axios(buildAccessTokenRequestUrl(shop), {
method: 'POST',
data
});
const fetchShopData = async (shop, accessToken) => await axios(buildShopDataRequestUrl(shop), {
method: 'GET',
headers: {
'X-Shopify-Access-Token': accessToken
}
});
///////////// Route Handlers /////////////
app.get('/shopify', (req, res) => {
const shop = req.query.shop;
if (!shop) { return res.status(400).send('no shop')}
const state = nonce();
const installShopUrl = buildInstallUrl(shop, state, buildRedirectUri())
res.cookie('state', state) // should be encrypted in production
res.redirect(installShopUrl);
});
app.get('/shopify/callback', async (req, res) => {
const { shop, code, state } = req.query;
const stateCookie = cookie.parse(req.headers.cookie).state;
if (state !== stateCookie) { return res.status(403).send('Cannot be verified')}
const { hmac, ...params } = req.query
const queryParams = querystring.stringify(params)
const hash = generateEncryptedHash(queryParams)
if (hash !== hmac) { return res.status(400).send('HMAC validation failed')}
try {
const data = {
client_id: shopifyApiPublicKey,
client_secret: shopifyApiSecretKey,
code
};
const tokenResponse = await fetchAccessToken(shop, data)
const { access_token } = tokenResponse.data
const shopData = await fetchShopData(shop, access_token)
res.send(shopData.data.shop)
} catch(err) {
console.log(err)
res.status(500).send('something went wrong')
}
});
///////////// Start the Server /////////////
app.listen(PORT, () => console.log(`listening on port ${PORT}`));
2-这是我的环境文件
SHOPIFY_API_PUBLIC_KEY=key here
SHOPIFY_API_SECRET_KEY=key here
3- 这是我的 package.json 文件
{
"name": "TestQasim",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.20.0",
"cookie": "^0.4.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"nodemon": "^2.0.4",
"nonce": "^1.0.4"
}
}
我使用 NGROK 隧道并使用此链接:
https://20a11edc124f.ngrok.io/shopify?shop=testqasim121312.myshopify.com
但得到上述错误。
我检查了 API URL 等,似乎也找不到任何问题。
这是错误的屏幕截图