node.js - 使用 request-promise 时无法设置 Koajs 的 ctx.status 和 ctx.body
问题描述
我目前正在编写验证 Shopify 应用程序的手动设置,类似于他们在NodeJS 和 Express中的设置方式。对于应用程序,我使用的是 NodeJS 和Koa框架。我还使用@koa/router进行路由,并尝试使用 request-promises 发出外部 HTTP 请求。我不能使用 Shopify 的 Koa Authentication 中间件,因为它不能与路由器一起使用,并且我需要让代码库在未来支持其他电子商务平台(BigCommerce、Magento 等)。
当我获得访问令牌或错误消息时,我无法在 Promise 和方法中更改 Koa 的 ctx.status 和.then()
ctx.body .catch()
。就好像它只是被忽略了一样。
我有以下设置来获取访问令牌:
const KoaRouter = require('@koa/router'),
const nonce = require('nonce')();
const querystring = require('querystring');
const crypto = require('crypto');
const dotenv = require('dotenv').config();
const request = require('request-promise');
const koaRequest = require('koa-http-request');
// Get Shopify information from the .env file
const apiKey = process.env.SHOPIFY_API_KEY;
const apiSecret = process.env.SHOPIFY_API_SECRET;
const scopes = process.env.SHOPIFY_SCOPE;
const router = new KoaRouter();
const getInstallURL = (shop) => {
const state = nonce();
const forwardingAddress = 'https://aecd64c2.ngrok.io'
const redirectUri = forwardingAddress + '/shopify/auth/callback';
return {
"url": 'https://' + shop +
'/admin/oauth/authorize?client_id=' + apiKey +
'&scope=' + scopes +
'&state=' + state +
'&redirect_uri=' + redirectUri,
"state": state
};
}
const verifyHMAC = (query, hmac) => {
const map = Object.assign({}, query);
delete map['hmac'];
const message = querystring.stringify(map);
const providedHmac = Buffer.from(hmac, 'utf-8');
const generatedHash = Buffer.from(
crypto
.createHmac('sha256', apiSecret)
.update(message)
.digest('hex'),
'utf-8'
);
let hashEquals = false;
// timingSafeEqual will prevent any timing attacks. Arguments must be buffers
try {
hashEquals = crypto.timingSafeEqual(generatedHash, providedHmac)
// timingSafeEqual will return an error if the input buffers are not the same length.
} catch (e) {
hashEquals = false;
};
return hashEquals;
};
const requestAccessToken = (shop, code) => {
const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
const accessTokenPayload = {
client_id: apiKey,
client_secret: apiSecret,
code,
};
console.log('Requesting accessToken for ' + shop + "!");
return request.post(accessTokenRequestUrl, { json: accessTokenPayload });
};
router.get('/shopify/auth', async (ctx, next) => {
const shop = ctx.request.query.shop;
console.log(shop);
if (shop) {
const ret = getInstallURL(shop);
console.log(ret)
ctx.cookies.set('state', ret['state']);
ctx.redirect(ret['url']);
} else {
ctx.status=400;
ctx.body='Missing shop parameter. Please add ?shop=your-development-shop.myshopify.com to your request';
}
});
router.get('/shopify/auth/callback', async (ctx, next) => {
const { shop, hmac, code, state } = ctx.request.query;
const stateCookie = ctx.cookies.get('state');
console.log(ctx.request.query);
console.log(stateCookie);
if (state !== stateCookie) {
ctx.status = 403;
ctx.body='Request origin cannot be verified';
}
if (shop && hmac && code) {
if (!verifyHMAC(ctx.request.query, hmac)) {
ctx.status = 400;
ctx.body='HMAC validation failed';
}
console.log('Verified HMAC. Now need to get access token');
requestAccessToken(shop, code).then(
(accessTokenResponse) => {
const accessToken = accessTokenResponse.access_token;
console.log('Access Token = ' + accessToken);
ctx.status= 200;
ctx.body= "Got an access token, let's do something with it";
}
).catch(
(error) => {
console.log('An error has occurred!');
console.log(error.statusCode);
console.log(error.error.error_description);
console.log(ctx.status);
ctx.status = error.statusCode;
ctx.body= error.error.error_description;
console.log(ctx.status);
}
)
} else {
ctx.status = 400;
ctx.body='Required parameters missing';
}
});
module.exports = router;
如何让 Koa 的上下文变量在 Promise 中工作?
解决方案
你可以编辑你的requestAccessToken
功能如下。.then()
如果函数未定义为异步,则不能使用方法。
const requestAccessToken = async (shop, code) => {
const accessTokenRequestUrl = 'https://' + shop + '/admin/oauth/access_token';
const accessTokenPayload = {
client_id: apiKey,
client_secret: apiSecret,
code,
};
console.log('Requesting accessToken for ' + shop + "!");
return await request.post(accessTokenRequestUrl, { json: accessTokenPayload });
};
推荐阅读
- python-3.x - 在 Window 7 上运行“从 skimage 导入数据”代码时发现“ModuleNotFoundError”
- dc.js - 为什么在折线图的上方和下方都呈现黑色区域?
- c - Xcode 中的 Git 忽略
- r - 如何在 R 的向量中包含变量?
- java - 如何在代码中的确切位置将数据从 firebase 提取到 android studio 中?
- python - 图聚类的 Louvain 算法在 Spark/Scala 和 Python 中运行时给出完全不同的结果,为什么会发生这种情况?
- python - 熊猫如何避免在 groupby nlargest n 中应用
- r - 有没有办法让 RMarkdown 选项卡即使在添加 Shiny 运行时也能显示?
- python - 获取绝对路径时出现问题?
- jquery - 使用Jquery离开ID div时如何清除mousemove