NodeJS 使用节点路由确认AWS SNS订阅

doinxwow  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(87)

我希望身体前进,从AWS SNS订阅确认发送POST请求到我的nodejs API路由,但身体是空的或未定义的
这是来自AWS的curl请求

curl -X 'POST' 'https://testemails.medixcel.in:5000/api/SESNotifications' -H 'connection: close' -H 'accept-encoding: gzip,deflate' -H 'user-agent: Amazon Simple Notification Service Agent' -H 'host: webhook.site' -H 'content-length: 1525' -H 'content-type: text/plain; charset=UTF-8' -H 'x-amz-sns-topic-arn: arn:aws:sns:ap-south-1:358988201836:medixcelnew' -H 'x-amz-sns-message-id: a27cafbb-ac8b-456a-835b-3c79915eaf68' -H 'x-amz-sns-message-type: SubscriptionConfirmation' -d $'{
  "Type" : "SubscriptionConfirmation",
  "MessageId" : "a27cafbb-ac8b-456a-835b-3c79915eaf68",
  "Token" : "2336412f37fb687f5d51e6e2425c464de63b4730d3a215f236ee1a2ac58cfe38eeb8567e697e09a421d6e7465b283cf57427cc65ac1eadda93c03250599aef69dae60496395586f1eea58bca586ded73f5c0fd9401580863db148fce0c4f4183cba43310475067be6dc3b23176a01253",
  "TopicArn" : "arn:aws:sns:ap-south-1:358988201836:medixcelnew",
  "Message" : "You have chosen to subscribe to the topic arn:aws:sns:ap-south-1:358988201836:medixcelnew.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
  "SubscribeURL" : "https://sns.ap-south-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:ap-south-1:358988201836:medixcelnew&Token=2336412f37fb687f5d51e6e2425c464de63b4730d3a215f236ee1a2ac58cfe38eeb8567e697e09a421d6e7465b283cf57427cc65ac1eadda93c03250599aef69dae60496395586f1eea58bca586ded73f5c0fd9401580863db148fce0c4f4183cba43310475067be6dc3b23176a01253",
  "Timestamp" : "2023-03-13T11:35:52.918Z",
  "SignatureVersion" : "1",
  "Signature" : "Pbysx3IgCD/4EzJFsZCuwUW7PsaZb/oO9EK2/woeJH+8FwYaBfmV5NpyBC9mhSGzg0kLJQY6tb9tadIm+7GPDqbNdwzeXUv4AkzydPdof6fomkkmE0XCxGK4NSRn044byBWnMnTGtTv1dOinwUYHaoV5pmX4y3eNBtWPxR7qE4F+zfBqzEcGrtbrDUkZNHn6t5w/3NWappiL/PJoAaZRWiSAQ8kWWq1fVdmwcs9EXEZVP8JJ75R2sSKEzOcQIv3CT96MODTFzcy8ErG0F3jYffbQs28+7mkyAng3IkT1o9oBNndJKGKMFj8DakYYd5EI+UIwubJ6C8jfZNHLgbYJ/Q==",
  "SigningCertURL" : "https://sns.ap-south-1.amazonaws.com/SimpleNotificationService-56e67fcb41f6fec09b0196692625d385.pem"
}'
fiei3ece

fiei3ece1#

如果您尝试使用https订阅SNS,以下几个步骤可能会对您有所帮助。

  • 首先,您需要创建一个路由。
  • 在AWS控制台中传递该路由并选择https订阅。
  • 当您在AWS中使用该路由创建订阅时AWS在该路由上发送验证,这是您的代码,在它包含SubscribeURL的有效负载中,您必须手动或通过代码库访问此URL,就像我对节点包request所做的那样,您可以使用request/axios/https,无论您喜欢什么,我使用request。
  • 在Get请求之后,它应该成功订阅。
const express = require("express");
const app = express();
const request = require('request');

app.use(express.text());

app.post("/", (req, res) => {
  const jsonParse = JSON.parse(req.body);
  console.log(jsonParse.SubscribeURL)
  console.log(jsonParse.Type)

  const promise = new Promise((resolve, reject) => {
      const url = jsonParse.SubscribeURL;

      request(url, (err, res) =>{
          if(!err && res.statusCode == 200){
              return resolve();
          }else{
              return reject();
          }
      });

  });
});

app.listen(3000, () => console.log("Server listening on port 3000!"));

相关问题