gpt4 book ai didi

node.js - SNS confirmSubscription 使用 NodeJs 和 Express

转载 作者:行者123 更新时间:2023-12-02 20:19:03 24 4
gpt4 key购买 nike

我正在尝试设置对 SNS 主题的订阅,我在 SNS UI 中添加了 HTTP 订阅,它被标记为“待确认”。我已经设置了我的端点来查找正确的 header (如下所示)并且它正在工作。我的问题是端点永远不会获得“ token ”,请参阅下面我的代码和日志的输出。

用于测试的 NodeJS(w/express 4.15)代码:

router.post('/', (req, res, next) => {
console.log('-----------------')
console.log(req.body)
console.log(req.headers)
console.log('-----------------')
if (req.get('x-amz-sns-message-type') == "SubscriptionConfirmation") {
SNS.confirmSubscription({ Token: req.body.Token, TopicArn: req.get('x-amz-sns-topic-arn') }, (err, finished) => {
if (err) console.log(err)
})
} else {
console.log(req.body)
}
})

来自 AWS 的 POST 请求

-----------------
{}
{ host: 'myapp.us-west-2.elasticbeanstalk.com',
'x-real-ip': '172.XXX.40.XXX',
'x-forwarded-for': '54.240.XXX.XXX, 172.XXX.40.XXX',
'content-length': '1604',
'accept-encoding': 'gzip,deflate',
'content-type': 'text/plain; charset=UTF-8',
'user-agent': 'Amazon Simple Notification Service Agent',
'x-amz-sns-message-id': '493b2a00-a99f-4cf3-ac9a-aad8ad6ee115',
'x-amz-sns-message-type': 'SubscriptionConfirmation',
'x-amz-sns-topic-arn': 'arn:aws:sns:us-west-2:4868XXXXXXXX:mysnstopic',
'x-forwarded-port': '80',
'x-forwarded-proto': 'http' }
-----------------

{ InvalidParameter: Invalid token
at Request.extractError (/var/app/current/node_modules/aws-sdk/lib/protocol/query.js:47:29)
at Request.callListeners (/var/app/current/node_modules/aws-sdk/lib/sequential_executor.js:105:20)
at Request.emit (/var/app/current/node_modules/aws-sdk/lib/sequential_executor.js:77:10)
at Request.emit (/var/app/current/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/var/app/current/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/app/current/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/app/current/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/app/current/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/app/current/node_modules/aws-sdk/lib/request.js:685:12)
at Request.callListeners (/var/app/current/node_modules/aws-sdk/lib/sequential_executor.js:115:18)
message: 'Invalid token',
code: 'InvalidParameter',
time: 2018-08-13T17:32:15.516Z,
requestId: '6945a62f-c746-5785-8497-f5c4059943ab',
statusCode: 400,
retryable: false,
retryDelay: 50.366310913093 }

在上面,您会看到 req.body 是空的,并且设置了 header ,但没有包含 Token。有人可以指出我做错了什么吗?

最佳答案

您必须明确通知 express 对此端点使用文本解析。

试试这个:

    const express = require('express');
const router = express.Router();
const request = require('request');
var bodyParser = require('body-parser')


router.post('/',bodyParser.text(),handleSNSMessage);
module.exports = router;


var handleSubscriptionResponse = function (error, response) {
if (!error && response.statusCode == 200) {
console.log('Yess! We have accepted the confirmation from AWS');
}
else {
throw new Error(`Unable to subscribe to given URL`);
//console.error(error)
}
}
async function handleSNSMessage(req, resp, next) {

try {
let payloadStr = req.body
payload = JSON.parse(payloadStr)
console.log(JSON.stringify(payload))
if (req.header('x-amz-sns-message-type') === 'SubscriptionConfirmation') {
const url = payload.SubscribeURL;
await request(url, handleSubscriptionResponse)
} else if (req.header('x-amz-sns-message-type') === 'Notification') {
console.log(payload)
//process data here
} else {
throw new Error(`Invalid message type ${payload.Type}`);
}
} catch (err) {
console.error(err)
resp.status(500).send('Oops')
}
resp.send('Ok')
}

编辑:从 express v4.17.0 开始,您可以使用 express.text() 中间件本身,而不是 bodyParser.text()。看看this official doc

注意:我没有使用 app.use,因为这会影响我的所有其他端点。

关于node.js - SNS confirmSubscription 使用 NodeJs 和 Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51828788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com