Quantcast
Channel: Service Bus forum
Viewing all articles
Browse latest Browse all 1916

Send message to queue gives the "40103: Invalid authorization token signature error"

$
0
0

I am new to the AZure.

I am trying to send a message to the service bus queue from javascript.

Below code gives me the "40103: Invalid authorization token signature error".Could you please help on this

I followed this article

http://developers.de/blogs/damir_dobric/archive/2014/03/18/windows-azure-service-bus-sending-the-message-to-queue-and-topic-with-javascript.aspx

  <script type="text/javascript">
        var m_ServiceNamespace = "MyQueue-ns";
       // var m_ServiceNamespace = "myqueue-ns";
        
        var m_SasKey = "tDkpA8LQ5OPoh/1fcWcBaVQZ6ZSByVxyasPsr+J4D4k="; //paste here key1     
        var m_SasKeyName = "RootManageSharedAccessKey";
        var environment = "servicebus.Windows.net";
        $(document).ready(function () {
            $("#btnSend").click(function () {
                alert("button");
                var txtMsg = "link"
                var msg = { "message": txtMsg, "id": 1234 };
                var queue = "myqueue";
                SB.sendMessage(queue, JSON.stringify(msg),
                  "application/json", function (messagingResult) {
                      var res = messagingResult;
                  });
            });
        });
        var getToken = function (entityPath) {

            var uri = "http://" + m_ServiceNamespace +
            ".servicebus.windows.net/" + entityPath;

            var endocedResourceUri = encodeURIComponent(uri);

            var t0 = new Date(1970, 1, 1, 0, 0, 0, 0);
            var t1 = new Date();
            var expireInSeconds = +(31 * 24 * 3600) + 3600 +
           (((t1.getTime() - t0.getTime()) / 1000) | 0);

            var plainSignature = escape(endocedResourceUri +"\n" + expireInSeconds)

            var hash = CryptoJS.HmacSHA256(plainSignature, m_SasKey);
            var base64HashValue = CryptoJS.enc.Base64.stringify(hash);

            var token = "SharedAccessSignature sr=" + endocedResourceUri + "&sig=" +
            encodeURIComponent(base64HashValue) + "&se=" + expireInSeconds + "&skn=" +
            m_SasKeyName;

            return token;
        }
        var utf8Encode = function (s) {
            for (var c, i = -1, l = (s = s.split("")).length,
                o = String.fromCharCode; ++i < l;
                s = (c = s.charCodeAt(0)) >= 127 ? o(0xc0 | (c >>> 6)) +
                o(0x80 | (c & 0x3f)) : s
            );
            return s.join("");
        }
        var SB = {
           sendMessage: function (entityPath, body, contentType, callback) {
               var securityToken = getToken(entityPath);
               console.log(securityToken);
                var entityUri = "https://" + m_ServiceNamespace + "." +
                environment + "/" + entityPath;
                var sendUri = entityUri + "/messages";
                var xmlHttpRequest = new XMLHttpRequest();

                xmlHttpRequest.open("POST", sendUri, true);
                xmlHttpRequest.setRequestHeader('Content-Type', contentType);
                xmlHttpRequest.setRequestHeader("Authorization", securityToken);
                xmlHttpRequest.onreadystatechange = function () {

                    if (this.readyState == 4) {

                        var messagingResult;

                        if (this.status == 201) {
                            messagingResult = new MessagingResult("Success",
                            this.status, null, this.response);
                        }
                        else {
                            //messagingResult = new MessagingResult("Failure",
                            //this.status, null, this.response);
                            console.log(this.status +"Message" +this.response);
                        }

                        if (callback != null)
                            callback(messagingResult);
                    }
                };

                xmlHttpRequest.send(body);
            }
                 }
    </script>


Viewing all articles
Browse latest Browse all 1916

Trending Articles