Я пытался следовать документации Amazon:
https://docs.aws.amazon.com/general/latest/gr/signature-version-2.html
https://docs.aws.amazon.com/sns/latest/api/API_Publish.html
но ответ на запрос всегда:
<ErrorResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
<Error>
<Type>Sender</Type>
<Code>InvalidClientTokenId</Code>
<Message>The security token included in the request is invalid</Message>
</Error>
<RequestId>********</RequestId>
</ErrorResponse>
В чем ошибка? Не правильная ли подпись? Как правильно отправить правильный запрос POST?
Это мой код:
var crypto = require('crypto-js');
var request = require('ajax-request');
AwsValues = {
accessKey:"**********",
secretKey:"**********"
region:"eu-central-1",
targetArn_iOS:"arn:aws:sns:eu-central-1:*************:endpoint/APNS_SANDBOX/*******************"
};
var message = {
default: 'default',
APNS_SANDBOX: {
aps: {
alert: 'Hello World',
sound: 'default',
badge: 1
}
},
APNS: {
aps: {
alert: 'Hello World',
sound: 'default',
badge: 1
}
}
};
message.APNS_SANDBOX = JSON.stringify(message.APNS_SANDBOX);
message.APNS = JSON.stringify(message.APNS);
message = JSON.stringify(message);
function hmacSHA256(string, key) {
return crypto.HmacSHA256(string, key).toString(crypto.enc.Base64);
}
var timeStamp = (new Date()).toISOString();
var targetArn = AwsValues.targetArn_iOS;
var awsAccessKeyId = AwsValues.accessKey;
var key = AwsValues.secretKey;
var params =
"AWSAccessKeyId=" + awsAccessKeyId + "&" +
"Action=Publish" + "&" +
"SignatureMethod=HmacSHA256" + "&" +
"SignatureVersion=2" + "&" +
"Timestamp=" + encodeURIComponent(timeStamp) + "&" +
"Version=" + "2010-03-31";
var url = "sns.eu-central-1.amazonaws.com";
var sign = "POST\n" + url + "\n" + "/\n" + params;
var signature = encodeURIComponent(hmacSHA256(sign,key));
url =
"http://" + url + "?" +
params + "&" +
"Signature=" + signature + "&" +
"Message=" + encodeURIComponent(message)+ "&" +
"TargetArn=" + encodeURIComponent(targetArn) + "&" +
"MessageStructure=json";
request({
url: url,
method: 'POST',
}, function(err, res, body) {
console.log(err);
console.log(res);
console.log(body);
console.log("url\n",url,"\n");
console.log("sign\n",sign,"\n");
console.log("signature\n",sign,"\n");
}
);