如何从SNS订阅中提取令牌并在ConfirmSubscription方法中使用它?

时间:2019-03-21 17:31:38

标签: amazon-web-services amazon-sns

用例

我想订阅SNS主题的电子邮件,我需要以编程方式进行确认,而不是手动单击确认链接。

我所做的

这通常是为了确认,我使用的是SNS confirmSubscription方法,该方法将token作为参数。我可以通过将协议传递为subscribe来调用SNS email-json方法来获得令牌。为此,我在端点处收到了以下订阅消息。

{
  "Type" : "SubscriptionConfirmation",
  "MessageId" : "xxx-9160-49a3-960f-xx",
  "Token" : "2336412f37fb687adfaf",
  "TopicArn" : "arn:aws:sns:ap-south-1:xx-xx",
  "Message" : "You have chosen to subscribe to the topic arn:aws:sns:ap-south-1:xx-xx.\nTo confirm the subscription, visit the SubscribeURL included in this message.",
  "SubscribeURL" : "https://sns.ap-south-1.amazonaws.com/?Action=xx&TopicArn=arn:aws:sns:ap-south-1:477530015769:xx-xx&Token=2336412f37fb6872",
  "Timestamp" : "2019-03-21T08:34:26.597Z",
  "SignatureVersion" : "1",
  "Signature" : "bIwq8Q92Yvrn8bBrIDQJ2chc5y/5IPAZPPR4roZ6WfK7kaE0QTbJpqAmHMH0sHL4aoyTrapLwX4e6HaEd6nGny8EAWj5wA2JvFCrO9BFQu",
  "SigningCertURL" : "https://sns.ap-south-1.amazonaws.com/SimpleNotificationService-xx.pem"
}

我想要的

如何能够从上述json中提取token值。这是我用来以编程方式确认sns订户的以下lambda。

const AWS = require('aws-sdk');
var sns = new AWS.SNS();
var snsconfirm= {
  Token: 'xx', /* required */ -->How can i able to retrieve the token value from the endpoint json.
  TopicArn: 'arn:aws:sns:ap-south-1:xxx:xx-xxx', /* required */
  AuthenticateOnUnsubscribe: 'true'
};
var snsparams = {
  Protocol: 'email-json', /* required */
  TopicArn: 'arn:aws:sns:ap-south-1:xx:xx-xx', /* required */
  Endpoint: 'abc@xyz.com'
};
exports.handler = async (event) => {
    // TODO implement
try {
  const snsrespone = await sns.subscribe(snsparams).promise();
  const confirmsub = await sns.confirmSubscription(snsconfirm).promise();
  } catch (err) {
    console.log(err.message);
    throw err;
  }
};

感谢任何帮助 谢谢

1 个答案:

答案 0 :(得分:0)

如您所知,只有拥有令牌的您才能以编程方式确认订阅。这最有可能确保您不会不愿意订阅某人。

假设您有权访问端点,您唯一的选择是设置某种过滤器,该过滤器触发可以订阅答复的Lambda。可能是通过在电子邮件中设置电子邮件转发规则以转发到SES电子邮件,从而触发Lambda来确认订阅。