Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SNS: Fetching the Token for confirmSubscription() #724

Closed
pree011235 opened this issue Sep 18, 2015 · 9 comments
Closed

SNS: Fetching the Token for confirmSubscription() #724

pree011235 opened this issue Sep 18, 2015 · 9 comments
Labels
guidance Question that needs advice or information.

Comments

@pree011235
Copy link

Trying to do a seemingly simple confirm subscription but I keep hitting the Invalid Token 400 issue:

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, /* Where can I get this from?? */
          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));

Where can I get the endpoint Token from, right after the subscribe (Tried to look up my this but that didn't have it either? Thanks!

@jeskew
Copy link
Contributor

jeskew commented Sep 18, 2015

Hi @pree011235,

The token will be provided in a message sent to the endpoint you're subscribing to an SNS channel. The endpoint will receive a POST message with a JSON payload containing, among other fields, a Token parameter and a SubscribeURL parameter. To confirm a subscription, you can perform a GET request on the value provided for SubscribeURL or use the provided Token to call sns.confirmSubscription.

@AdityaManohar
Copy link
Contributor

@pree011235 I'm marking this as closed as it is not an SDK issue. As @jeskew mentioned, the token parameter is supplied in a confirmation message supplied to the endpoint that has been subscribed to receive SNS notifications.

Feel free to reopen this issue or open another issue if you have other questions.

@aadamsx
Copy link

aadamsx commented Oct 11, 2015

I'm trying to do this same thing, using the sms protocol. I too was placing the confirmSubscription in the subscribe callback. @pree011235 in my case, when I call the subscribe, my test phone gets the SMS message and I have to reply YES in order to confirm the subscribe. From a coding standpoint, How do I confirm that phone subscribed?

@jeskew
Copy link
Contributor

jeskew commented Oct 11, 2015

@aadamsx You can call listSubscriptions and check to see if your test phone has been successfully subscribed.

@aadamsx
Copy link

aadamsx commented Oct 11, 2015

Thank you @jeskew

I'm going to use listSubscriptionsByTopic. But it only returns 100 subscriptions, requiring a NextToken in order to get more.

Do you have any sudo-code that would help?

This is what I came up with:

      AWS.config.update({
         accessKeyId: Meteor.settings.awsAccessKeyId,
         secretAccessKey: Meteor.settings.awsSecretKey,
         region: "us-east-1"
      });

      var sns = new AWS.SNS({params: {TopicArn: 'arn:aws:sns:us-east-1:34523452345:test'}});
      var nextToken = null;
      var index = 0;
      var results[];
      do {
        result = sns.ListSubscriptionsByTopic({NextToken: nextToken}, function (err, data) {
          if (err) console.log(err, err.stack); // an error occurred
          else {
            // call ListSubscriptionsByTopic again?  How?
            results[index] = data.Subscriptions;
            if (NextToken) {
              nextToken = NextToken;
              index = index + 1;
            } else {
              nextToken = null;
            }
          }
        });  
      }
      while (nextToken !== null);

    // do something with the results

@jeskew
Copy link
Contributor

jeskew commented Oct 13, 2015

Hi @aadamsx,

The callback passed to sns.ListSubscriptionsByTopic will be called by the operation's response, which has a hasNextPage method and a nextPage method. The latter takes a callback and handles pagination for you. You won't need to keep track of the nextToken.

var sns = new AWS.SNS({region: 'us-west-2'})
var paginate = function (err, data) {
        if (err) {
            console.log(err);
        } else {
            // current page of data is available as 'data'
            if (this.hasNextPage()) {
                this.nextPage(paginate);
            }
        }
    };

sns.listSubscriptions(paginate);

@aadamsx
Copy link

aadamsx commented Oct 13, 2015

Thank you, this is exactly the feedback I was looking for!

I got things working, but without the nextToken. I was confused what to do about it, and you just clear it up for me!

@desaiazaz-zz
Copy link

var nextToken = null;
var index = 0;
var results=[];
do {
results = sns.ListSubscriptionsByTopic({NextToken: nextToken}, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
// call ListSubscriptionsByTopic again? How?
results[index] = data.Subscriptions;
if (NextToken) {
nextToken = NextToken;
index = index + 1;
} else {
nextToken = null;
}
}
});
}
while (nextToken !== null);
console.log("LIST>>>",results);

got the error ListSubscriptionsByTopic function not found

@srchase srchase added guidance Question that needs advice or information. and removed Question labels Jan 4, 2019
@lock
Copy link

lock bot commented Sep 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

6 participants