cordova - Creating registration ID for Azure Notification Hub via REST api -


i'm building app in cordova, , want use azure notification hub send push notifications app. i've had issues notification hub cordova plugin, fails build 64-bit architecture - hence it's plugin not option me. instead i'm using notification hub rest api. i'm stuck trying create registration id device. i'm able send ajax-request notification hub, keep getting 401 error.

here's js code far:

    function registerhub() {   var connectionstring = 'endpoint=sb://[service-bus-name].servicebus.windows.net/;sharedaccesskeyname=defaultfullsharedaccesssignature;sharedaccesskey=[my-shared-access-key]';    var parts = connectionstring.split(';');   if(parts.length != 3)   throw "error parsing connection string";    parts.foreach(function(part){     if (part.indexof('endpoint') == 0){       endpoint = 'https' + part.substring(11);       console.log('endpoint ' + endpoint);     } else if(part.indexof('sharedaccesskeyname') == 0){       saskeyname = part.substring(20);       console.log('saskeyname ' + saskeyname);     } else if(part.indexof('sharedaccesskey') == 0) {       saskeyvalue = part.substring(16);       console.log('saskeyvalue ' + saskeyvalue);     }   });    var getselfsignedtoken = function(resourceuri, sharedkey, ruleid, expiresinmins) {     targeturi = encodeuricomponent(resourceuri.tolowercase()).tolowercase();      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;     console.log('expires in epoch: ' + expires);     var tosign = resourceuri + "\n" + expires;      var signature = cryptojs.hmacsha256(tosign, sharedkey);     var base64signature = signature.tostring(cryptojs.enc.base64);     var base64uriencoded = encodeuricomponent(base64signature);      console.log('signature: ' + base64uriencoded + ' tosign ' + tosign);      var token = "sharedaccesssignature sr=" + targeturi + "&sig=" + base64uriencoded + "&se=" + expires + "&skn=" + ruleid;      return token;   };    var targeturi = 'http://[service-bus-namespace].servicebus.windows.net/[notification-hub-name]';   var hubpath = [hub path];   var sharedkey = saskeyvalue;   var ruleid = saskeyname;   var expiresinmins = 129600;        var createregistrationid = function(targeturi, endpoint) {       console.log('create registration endpoint: ' + endpoint + ' targeturi ' + targeturi);       var registrationpath = hubpath + "/registrations/";       var serverurl = endpoint + registrationpath + "?api-version=2015-01";       console.log('serverurl ' + serverurl);        var token = getselfsignedtoken(targeturi, sharedkey, ruleid, expiresinmins);       var deferred = $.deferred();       $.ajax({         type: "post",         url: serverurl,         headers: {           "content-type": "application/atom+xml;type=entry;charset=utf-8",           "authorization" : token,           "x-ms-version": "2015-01"         },         beforesend: function(data){           console.log('before sending. current token: ' + token);         }       }).done(function(data, status, response) {         console.log('done response' + json.stringify(response) + ' status ' + json.stringify(status) + ' data ' + json.stringify(data));         var location = response.getresponseheader("content-location");         deferred.resolve(location);       }).fail(function(response, status, error) {         console.log("error: " + error + ' error status ' + json.stringify(status) + ' error response ' + json.stringify(response));         deferred.reject("error: " + error);       });        return deferred.promise();     };      var registrationid = createregistrationid(targeturi, endpoint);     console.log('regid: ' + json.stringify(registrationid));  } 

the posted javascript generates variables: "endpoint", "saskeyname" , "saskeyvalue". these variables getselfsignedtoken-function generates , returns token. far good. ajax-call notification hub keeps returning 401 error code: error: 40103: invalid authorization token complete json-response is:

{"readystate":4,"responsetext":"","status":401,"statustext":"40103: invalid authorization token signature"} 

i've followed code snippets azure notification hub rest api. common concepts: https://msdn.microsoft.com/en-us/library/azure/dn495627.aspx use rest api device: https://msdn.microsoft.com/en-us/library/azure/dn495631.aspx

i've tried using sas root key entire servicebus, i've tried calling /registrationids/ instead of /registrations/ (as stated in documentation creating registration id - confusing code snippets in link above, refers /registration/, i'm using in code above). i've tried "authorization": token in header of ajax-request, , x-ms-version , content-type (as in code above). same response.

i can't grasp if i'm using right serverurl reading azure documentation. i'm unsure if token generated correctly, although looks right when console.log before send. i'm sure i'm missing small detail, i've been twisting head around couple of days now. if created registration id via rest api, break down of token generation , how construct serverurl.

best regards

edit: forgot add, i'm testing on iphone 6, ios 8.4 (real device not emulator).

edit#2: token looks @ moment:

sharedaccesssignature sr=https%3a%2f%2f[service-bus-namespace].servicebus.windows.net%2f[notification-hub-name]&sig=7d164dcbeadd3c79248e94ece032cfb689c9890de4dc1e4959a961889860bb18&se=1444208043&skn=defaultfullsharedaccesssignature 

does right?

var hubname = '<enter hub name>'; var connectionstring = '<enter defaultfullsharedaccesssignatire>'; var apiversion = "?api-version=2015-01"; var expiresinmins = 129600;  var endpoint; var saskeyvalue; var saskeyname; var targeturi;  function parsehubconnectionstring() {      var parts = connectionstring.split(';');     if(parts.length != 3)         throw "error parsing connection string";      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);         }     });      targeturi = endpoint + hubname; }   var getselfsignedtoken = function (resourceuri, sharedkey, ruleid, expiresinmins) {     resourceuri = encodeuricomponent(resourceuri.tolowercase()).tolowercase();      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 = resourceuri + "\n" + expires;      // using cryptojs     var signature = cryptojs.hmacsha256(tosign, sharedkey);     var base64signature = signature.tostring(cryptojs.enc.base64);     var base64uriencoded = encodeuricomponent(base64signature);      var token = "sharedaccesssignature sr=" + resourceuri + "&sig=" + base64uriencoded + "&se=" + expires + "&skn=" + ruleid;      return token; };     var createregistrationid = function(uri, endpoint) {      var registrationpath = uri + "/registrations/";     var resourceuri = registrationpath + apiversion;      var token = getselfsignedtoken(resourceuri, saskeyvalue, saskeyname, expiresinmins);     var deferred = $.deferred();     $.ajax({         type: "post",         url: resourceuri,         data: getchanneldata(),         headers: {             "content-type": "application/atom+xml;type=entry;charset=utf-8",             "authorization": token,             "x-ms-version": "2015-01"         },         beforesend: function(data){             console.log('before sending. current token: ' + token);         }     }).done(function(data, status, response) {         console.log('done response' + json.stringify(response) + ' status ' + json.stringify(status) + ' data ' + json.stringify(data));         var location = response.getresponseheader("content-location");         deferred.resolve(location);     }).fail(function(response, status, error) {         console.log("error: " + error + ' error status ' + json.stringify(status) + ' error response ' + json.stringify(response));         deferred.reject("error: " + error);     });      return deferred.promise(); }; 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -