dimanche 25 mai 2014

Android - enregistrement de créer à l'aide des API Microsoft Notification moyeux reste - Stack Overflow


I have been trying to implement notification hubs in amy PhoneGap application. Since it is phonegap we have no other option rather than using Notification Hubs REST APIs. SO i have the following


var REQUEST_URI = "https://{namespace}.servicebus.windows.net/{NotificationHub}/registrations/?api-version=2013-08";

var token = global_getSelfSignedToken(REQUEST_URI, 60);
console.log("token: " + token);
var data = '<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">\
<content type="application/xml">\
<GcmRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">\
<Tags>myTag, myOtherTag</Tags>\
<GcmRegistrationId>{GCM Registration Id}</GcmRegistrationId> \
</GcmRegistrationDescription>\
</content>\
</entry>';
$.ajax({
type: "POST",
url: REQUEST_URI,
data: data,
headers: {
"Authorization": token,
"Content-Type": 'application/atom+xml;type=entry;charset=utf-8',
"x-ms-version": "2013-08"
},
beforeSend: function() {
console.log("Trying to reach Notification Hub");
},
success: function(data, status, response) {
console.log("success!!!");
},
error: function(response, status, error) {
console.log("status: " + status);
console.log("error: " + error);
}
});

And global_getSelfSignedToken Method as


function global_getSelfSignedToken(targetUri,
expiresInMins) {
targetUri = encodeURIComponent(targetUri.toLowerCase()).toLowerCase();
//prepare SAS key
var parts = NOTOFICATION_HUB_FULL_ACCESS_KEY.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) {
sharedKey = part.substring(20);
} else if (part.indexOf('SharedAccessKey') == 0) {
ruleId = part.substring(16);
}
});

// 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, sharedKey);
var base64signature = signature.toString(CryptoJS.enc.Base64);
var base64UriEncoded = encodeURIComponent(base64signature);

// construct autorization string
var token = "SharedAccessSignature sr=" + targetUri + "&sig="
+ base64signature + "&se=" + expires + "&skn=" + ruleId;
// console.log("signature:" + token);
return token;
}

References: http://msdn.microsoft.com/en-us/library/windowsazure/dn223265.aspx http://msdn.microsoft.com/en-us/library/windowsazure/dn495631.aspx


Problem: I always get response as Unauthorized


Anybody ever tried NotificationHubs with phonegap ?



I have been trying to implement notification hubs in amy PhoneGap application. Since it is phonegap we have no other option rather than using Notification Hubs REST APIs. SO i have the following


var REQUEST_URI = "https://{namespace}.servicebus.windows.net/{NotificationHub}/registrations/?api-version=2013-08";

var token = global_getSelfSignedToken(REQUEST_URI, 60);
console.log("token: " + token);
var data = '<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">\
<content type="application/xml">\
<GcmRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">\
<Tags>myTag, myOtherTag</Tags>\
<GcmRegistrationId>{GCM Registration Id}</GcmRegistrationId> \
</GcmRegistrationDescription>\
</content>\
</entry>';
$.ajax({
type: "POST",
url: REQUEST_URI,
data: data,
headers: {
"Authorization": token,
"Content-Type": 'application/atom+xml;type=entry;charset=utf-8',
"x-ms-version": "2013-08"
},
beforeSend: function() {
console.log("Trying to reach Notification Hub");
},
success: function(data, status, response) {
console.log("success!!!");
},
error: function(response, status, error) {
console.log("status: " + status);
console.log("error: " + error);
}
});

And global_getSelfSignedToken Method as


function global_getSelfSignedToken(targetUri,
expiresInMins) {
targetUri = encodeURIComponent(targetUri.toLowerCase()).toLowerCase();
//prepare SAS key
var parts = NOTOFICATION_HUB_FULL_ACCESS_KEY.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) {
sharedKey = part.substring(20);
} else if (part.indexOf('SharedAccessKey') == 0) {
ruleId = part.substring(16);
}
});

// 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, sharedKey);
var base64signature = signature.toString(CryptoJS.enc.Base64);
var base64UriEncoded = encodeURIComponent(base64signature);

// construct autorization string
var token = "SharedAccessSignature sr=" + targetUri + "&sig="
+ base64signature + "&se=" + expires + "&skn=" + ruleId;
// console.log("signature:" + token);
return token;
}

References: http://msdn.microsoft.com/en-us/library/windowsazure/dn223265.aspx http://msdn.microsoft.com/en-us/library/windowsazure/dn495631.aspx


Problem: I always get response as Unauthorized


Anybody ever tried NotificationHubs with phonegap ?


0 commentaires:

Enregistrer un commentaire