在Azure Notification Hub上注册会返回401 ERR_Aborted错误

时间:2018-10-02 13:10:28

标签: jquery ajax azure

我正在尝试通过JavaScript(jQuery AJAX)注册/订阅我的Azure通知中心,但是我得到了ERR_ABORTED 401回调返回

我的AJAX通话是

  function sendNHRegistrationRequest()
 {
    var deferred = $.Deferred();
    $.ajax({
        type: "POST",
        url: originalUri + "/registrations/?api-version=2014-09",
        dataType: "jsonp",
        headers: {
            "Content-Type": "application/atom+xml;type=entry;charset=utf-8",
            "Authorization": sasToken,
            "x-ms-version": "2015-01"
        },

sasToken在哪里

  

SharedAccessSignature   sr = https%3a%2f%2fmydomain.servicebus.windows.net%2fpnc-myapp-test&sig = 9g3EPtKq%2FUKv26MQ%2B5ceL11z7f4WySrQ16U61iXA%2BzU%3D&se = 1538544653&skn = DefaultFullShared>

它是使用CryptoJS针对我的初始connectionString

派生的
 function splitConnectionString()
 {
   var parts = connectionString.split(';');
   if (parts.length != 3)
   {console.log("Error with connection string");
   } else {

   parts.forEach(function(part) {
     if (part.indexOf('Endpoint') == 0) {
     endpoint = 'https' + part.substring(11);
     } else if (part.indexOf('SharedAccessKeyName') == 0) {
     sasKeyName = part.substring(20);
     } else if (part.indexOf('SharedAccessKey') == 0) {
     sasKeyValue = part.substring(16);
     }
   });
   }

   originalUri = endpoint + hubName;
 }

 function generateSaSToken()
 {
   targetUri = encodeURIComponent(originalUri.toLowerCase()).toLowerCase();
   var expiresInMins = 1000; // 1000 minute expiration

   // Set expiration in seconds.
   var expireOnDate = new Date();
   expireOnDate.setMinutes(expireOnDate.getMinutes() + expiresInMins);
   var expires = Date.UTC(expireOnDate.getUTCFullYear(), expireOnDate
     .getUTCMonth(), expireOnDate.getUTCDate(), expireOnDate
     .getUTCHours(), expireOnDate.getUTCMinutes(), expireOnDate
     .getUTCSeconds()) / 1000;
   var tosign = targetUri + '\n' + expires;

   // Using CryptoJS.
   var signature = CryptoJS.HmacSHA256(tosign, sasKeyValue);
   var base64signature = signature.toString(CryptoJS.enc.Base64);
   var base64UriEncoded = encodeURIComponent(base64signature);

   // Construct authorization string.
   sasToken = "SharedAccessSignature sr=" + targetUri + "&sig="
                   + base64UriEncoded + "&se=" + expires + "&skn=" + sasKeyName;

 }

我看到了围绕ServerKey的讨论-我需要在哪里将ServerKey合并到我的请求中?

0 个答案:

没有答案