通过REST API的Azure通知中心创建的注册ID [英] Creating registration ID for Azure Notification Hub via REST api

查看:442
本文介绍了通过REST API的Azure通知中心创建的注册ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建在科尔多瓦的应用程序,我想使用的Azure通知中心发送推送通知的应用程序。我已经与通知中心科尔多瓦插件的一些问题,因为它不能建立64位架构 - 因此它的这个插件是不是我的选择。
相反,我使用的通知中心REST API。但是我又立刻陷入试图创建一个从设备注册ID。我能够发送一个AJAX请求到我的通知中心,但我不断收到一个401错误。

下面是我的js code迄今:

 函数registerHub(){
  VAR的connectionString = 'Endpoint=sb://[service-bus-name].servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=[my-shared-access-key]';  VAR部分= connectionString.split(';');
  如果(parts.length!= 3)
  抛出错误解析连接字符串;  parts.forEach(函数(部分){
    如果(part.indexOf('端点')== 0){
      终点='HTTPS'+ part.substring(11);
      的console.log('端点'+终端);
    }否则如果(part.indexOf('SharedAccessKeyName')== 0){
      sasKeyName = part.substring(20);
      的console.log('sasKeyName'+ sasKeyName);
    }否则如果(part.indexOf('SharedAccessKey')== 0){
      sasKeyValue = part.substring(16);
      的console.log('sasKeyValue'+ sasKeyValue);
    }
  });  VAR getSelfSignedToken =功能(resourceUri,sharedKey,ruleId,expiresInMins){
    targetUri = EN codeURIComponent(resourceUri.toLowerCase())与toLowerCase()。    VAR expireOnDate =新的日期();
    expireOnDate.setMinutes(expireOnDate.getMinutes()+ expiresInMins);
    变种期满= Date.UTC(expireOnDate.getUTCFullYear(),expireOnDate.getUTCMonth(),expireOnDate.getUTCDate(),expireOnDate.getUTCHours(),expireOnDate.getUTCMinutes(),expireOnDate.getUTCSeconds())/ 1000;
    的console.log('到期后时代:'+到期);
    VAR tosign = resourceUri +\\ n+届满;    VAR签名= CryptoJS.HmacSHA256(tosign,sharedKey);
    VAR base64signature = signature.toString(CryptoJS.enc.base64);
    VAR base64UriEn codeD = EN codeURIComponent(base64signature);    的console.log('签名:'+ base64UriEn codeD +'tosign'+ tosign);    VAR令牌=SharedAccessSignature SR =+ targetUri +&放大器; SIG =+ base64UriEn codeD +&放大器; SE =+到期+&放大器; SKN =+ ruleId;    返回记号。
  };  VAR targetUri =的http:// [服务总线命名空间] .servicebus.windows.net / [通​​知毂名]';
  VAR hubPath = [枢纽路径]
  VAR sharedKey = sasKeyValue;
  VAR ruleId = sasKeyName;
  VAR expiresInMins = 129600;    VAR createRegistrationId =功能(targetUri,端点){
      的console.log('创建注册端点:'+终端+'targetUri'+ targetUri);
      VAR registrationPath = hubPath +/注册/;
      VAR的serverUrl = +终端+ registrationPathAPI版本= 2015-01?
      的console.log('的serverUrl'+的serverUrl);      VAR令牌= getSelfSignedToken(targetUri,sharedKey,ruleId,expiresInMins);
      变种推迟= $ .Deferred();
      $阿贾克斯({
        键入:POST,
        网址:的serverUrl,
        标题:{
          内容类型:应用程序/原子+ XML;类型=项;字符集= UTF-8,
          授权:令牌,
          X-MS-版本:2015-01
        },
        beforeSend:功能(数据){
          的console.log('。在发送前当前令牌:'+令牌);
        }
      })。完成(功能(数据,状态,响应){
        的console.log('做了回应'+ JSON.stringify(响应)+'状态'+ JSON.stringify(状态)+'数据'+ JSON.stringify(数据));
        VAR位置= response.getResponseHeader(内容定位);
        deferred.resolve(位置);
      })失败(函数(响应状态,错误){
        的console.log(错误:+误差+错误状态+ JSON.stringify(状态)+'的错误响应'+ JSON.stringify(响应));
        deferred.reject(错误:+误差);
      });      返回deferred.promise();
    };    VAR registrationId = createRegistrationId(targetUri,终点);
    的console.log('REGID:'+ JSON.stringify(registrationId));}

在公布的JavaScript成功生成变量:终点,sasKeyName和sasKeyValue。
从这些变量我getSelfSignedToken功能生成并返回一个标记。到现在为止还挺好。
但是我的Ajax调用通知中枢不断返回401错误code:
错误:40103:无效的授权令牌
完整的JSON-反应是:

  {readyState的4,responseText的:,状态:401,状态文本:40103:无效的授权令牌签名}

我已经按照从Azure的通知中心REST API的code片段。
共同的概念: https://msdn.microsoft.com/en-us/库/天蓝色/ dn495627.aspx
从设备使用REST API: https://msdn.microsoft.com/en -us /库/天蓝色/ dn495631.aspx

我已经使用SAS根密钥我的整个servicebus试过,我也打过电话/ registrationIDs /不是/注册/(如文档中说明有关创建注册ID - 混淆为code片段在上面的链接,是指/报名/,我使用了code这上面)。
我试过,只有授权:令牌我的Ajax请求的头,并用X-MS-版本和内容类型(如上面的code)。
但总是以同样的反应。

如果我使用的是正确的serverUrl通过阅读天青文档我无法把握。还我不确定是否正确产生了USB Key,虽然看起来正确的,当我CONSOLE.LOG之前发送。
我敢肯定,我错过了一些小细节,但我一直在扭我解决这个头一两天。所以,如果有人通过REST API创建成功注册ID,我真的想休息令牌生成的下降,以及如何构建的serverUrl。

最好的问候

编辑:
忘了补充一点,我在iPhone 6的iOS 8.4(实际设备不是模拟器)测试。

编辑#2:
我的标记看起来像这样的时刻:

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

这是否正确?


解决方案

  VAR hubName ='<输入您的集线器名称>';
VAR的connectionString ='<输入DefaultFullSharedAccessSignatire>';
VAR apiVersion =API版本= 2015-01?
VAR expiresInMins = 129600;VAR端点;
VAR sasKeyValue;
VAR sasKeyName;
VAR targetUri;功能ParseHubConnectionString(){    VAR部分= connectionString.split(';');
    如果(parts.length!= 3)
        抛出错误解析连接字符串;    parts.forEach(函数(部分){
        如果(part.indexOf('端点')== 0){
            终点='HTTPS'+ part.substring(11);
        }否则如果(part.indexOf('SharedAccessKeyName')== 0){
            sasKeyName = part.substring(20);
        }否则如果(part.indexOf('SharedAccessKey')== 0){
            sasKeyValue = part.substring(16);
        }
    });    targetUri =端点+ hubName;
}
VAR getSelfSignedToken =功能(resourceUri,sharedKey,ruleId,expiresInMins){
    resourceUri = EN codeURIComponent(resourceUri.toLowerCase())与toLowerCase()。    VAR expireOnDate =新的日期();
    expireOnDate.setMinutes(expireOnDate.getMinutes()+ expiresInMins);
    变种期满= Date.UTC(expireOnDate.getUTCFullYear(),expireOnDate.getUTCMonth(),expireOnDate.getUTCDate(),expireOnDate.getUTCHours(),expireOnDate.getUTCMinutes(),expireOnDate.getUTCSeconds())/ 1000;
    VAR tosign = resourceUri +\\ n+届满;    使用CryptoJS //
    VAR签名= CryptoJS.HmacSHA256(tosign,sharedKey);
    VAR base64signature = signature.toString(CryptoJS.enc.Base64);
    VAR base64UriEn codeD = EN codeURIComponent(base64signature);    VAR令牌=SharedAccessSignature SR =+ resourceUri +&放大器; SIG =+ base64UriEn codeD +&放大器; SE =+到期+&放大器; SKN =+ ruleId;    返回记号。
};
VAR createRegistrationId =功能(URI,端点){    VAR registrationPath =乌里+/注册/;
    VAR resourceUri = registrationPath + apiVersion;    VAR令牌= getSelfSignedToken(resourceUri,sasKeyValue,sasKeyName,expiresInMins);
    变种推迟= $ .Deferred();
    $阿贾克斯({
        键入:POST,
        网址:resourceUri,
        数据:getChannelData(),
        标题:{
            内容类型:应用程序/原子+ XML;类型=项;字符集= UTF-8,
            授权:令牌,
            X-MS-版本:2015-01
        },
        beforeSend:功能(数据){
            的console.log('。在发送前当前令牌:'+令牌);
        }
    })。完成(功能(数据,状态,响应){
        的console.log('做了回应'+ JSON.stringify(响应)+'状态'+ JSON.stringify(状态)+'数据'+ JSON.stringify(数据));
        VAR位置= response.getResponseHeader(内容定位);
        deferred.resolve(位置);
    })失败(函数(响应状态,错误){
        的console.log(错误:+误差+错误状态+ JSON.stringify(状态)+'的错误响应'+ JSON.stringify(响应));
        deferred.reject(错误:+误差);
    });    返回deferred.promise();
};

I'm building an app in cordova, and i want to use an Azure Notification Hub to send push notifications to the app. I've had some issues with the Notification Hub Cordova Plugin, as it fails to build for 64-bit architecture - hence it's this plugin is not an option for me. Instead i'm using the Notification Hub REST api. However i'm stuck trying to create a registration id from the device. I'm able to send an AJAX-request to my Notification Hub, but i keep getting a 401 error.

Here's my js code so 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 successfully generates the variables: "endpoint", "sasKeyName" and "sasKeyValue". From these variables my getSelfSignedToken-function generates and returns a token. So far so good. However my ajax-call to the Notification Hub keeps returning a 401 error code: Error: 40103: Invalid authorization token The complete JSON-response is:

{"readyState":4,"responseText":"","status":401,"statusText":"40103: Invalid authorization token signature"}

I've followed the code snippets from the Azure Notification Hub REST api. Common Concepts: https://msdn.microsoft.com/en-us/library/azure/dn495627.aspx Use REST api from device: https://msdn.microsoft.com/en-us/library/azure/dn495631.aspx

I've tried using the SAS root key for my entire servicebus, i've also tried calling /registrationIDs/ instead of /registrations/ (as stated in the documentation about creating registration id - confusing as the code snippets in the link above, refers to /registration/, which i'm using in the code above). I've tried with only "Authorization": token in the header of my ajax-request, and with the x-ms-version and content-type (as in the code above). But always with the same response.

I can't grasp if i'm using the right serverUrl by reading the Azure Documentation. Also i'm unsure if my token is generated correctly, although it looks right when i console.log it before send. I'm sure that i'm missing some small detail, but i've been twisting my head around this for a couple of days now. So if anybody successfully created registration ID via the REST api, i would really like a break down of the token generation and how to construct the serverUrl.

Best regards

EDIT: Forgot to add, that i'm testing on iPhone 6, iOS 8.4 (real device not emulator).

EDIT#2: My token looks like this at the moment:

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

Does this look right?

解决方案

var hubName = '<Enter Your Hub Name>';
var connectionString = '<Enter your 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();
};

这篇关于通过REST API的Azure通知中心创建的注册ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆