简体   繁体   中英

AWS SNS Invalid Token

I am trying to have the endpoint as my website ( http://website.com/ ) and publish to the SNS topic after successful subscription.

No matter what I try, I get the same Invalid Token Error. Can someone help me out? (sanitized code)

 endPointTest()
  .then((result) => {
    console.log("Result", result);

    AWS.config.update({
      accessKeyId:'accessKeyIdvalue',
      secretAccessKey:'secretAccessKeyvalue',
      region: 'us-west-2'
    });
    var sns = new AWS.SNS();
    console.log("SNS OBj", sns); 
    var params2 = {
      Protocol: 'http', /* required */
      TopicArn: 'topic_ARN_from_console', /* required */
      Endpoint: 'http://website.com/'
    };
    sns.subscribe(params2, function(err, data) {
      if (err) console.log(err, err.code); // an error occurred, where it breaks
      else     {
        console.log("Subscribing...", data.ResponseMetadata.RequestId);//Prints this
        console.log("Subscribing... with data", data); //Prints this fine
        var params1 = {
          Token: data.ResponseMetadata.RequestId, /* required */
          TopicArn: 'topic_ARN_from_console' /* required */
          //AuthenticateOnUnsubscribe: 'false'
        };
        sns.confirmSubscription(params1, function(err, data) {
          if (err) console.log(err, err.stack); // an error occurred
          else    {
            console.log("Confirmed Sub", data);
          }          // successful response
        });
      }
    });
  }).catch(console.error.bind(console));

Error I get:

POST https://sns.us-west-2.amazonaws.com/ 400 (Bad Request)

VideoUploader.js:135 Error: Invalid token

Where am I going wrong?

The SNS subscribe() documentation says that the returned data only contains a SubscriptionArn. It does not contain the confirmation token.

I believe that the subscription confirmation token that you are looking for is sent by SNS to the endpoint that you indicated when you called subscribe(). This is how SNS confirms that you control the endpoint. You need to retrieve it from that endpoint and then provide it back to SNS by calling confirmSubscription().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM