在Postman中为Azure通知中心创建注册

Jér*_*oul 8 api rest azure-notificationhub postman

我在Azure门户中创建了一个服务总线/通知中心.

现在我正在尝试使用基于此doc的Postman的Azure REST API :https: //msdn.microsoft.com/en-us/library/azure/dn223265.aspx

这是我的Postman配置:

这是以下网址的POST方法(创建注册) https://mysite.servicebus.windows.net/mysite-notif/registrations/?api-version=2015-01 (出于隐私原因,我在该网址中替换为mysite)

在标题中,我输入了2个条目:

Content-Type application/atom + xml; type = entry; charset = utf-8

授权 Endpoint = sb:// [mysite] .servicebus.windows.net /; SharedAccessKeyName = DefaultFullSharedAccessSignature; SharedAccessKey = [mykey](这是我从Azure门户复制的连接信息)

在Body中,我选择了raw-XML(txt/xml)并粘贴:

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom">
    <content type="application/xml">
        <WindowsRegistrationDescription xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/netservices/2010/10/servicebus/connect">
            <Tags>myTag, myOtherTag</Tags>
            <ChannelUri>{ChannelUri}</ChannelUri>
        </WindowsRegistrationDescription>
    </content>
</entry>
Run Code Online (Sandbox Code Playgroud)

(这是Windows Notification Service示例的本机注册)

当我从邮递员中发送此电话时,我收到401错误:

<Error>
    <Code>401</Code>
    <Detail>MalformedToken: The credentials contained in the authorization header are not in the WRAP format..TrackingId:ee0d87ef-6175-46a1-9b35-6c31eed6049d_G2,TimeStamp:8/13/2015 9:58:26 AM</Detail>
</Error>
Run Code Online (Sandbox Code Playgroud)

我错过了什么?这是我留在邮递员"No Auth"的授权标签吗?它是应该如此处所示编码的Authorization标头的值吗? 通过REST API创建Azure通知中心的注册ID

谢谢.

Bar*_*kar 6

您的"授权"标头不正确.

如Azure通知中心REST API文档中所述,例如,用于创建注册,"授权"标头必须包含"使用服务总线进行共享访问签名身份验证中指定的令牌" ...

令牌格式在使用Service Bus进行共享访问签名身份验证的文档中指定,如下所示:

SharedAccessSignature sig=<signature-string>&se=<expiry>&skn=<keyName>&sr=<URL-encoded-resourceURI>
Run Code Online (Sandbox Code Playgroud)
  • URL-encoded-resourceURI:您发送POST请求的URL(在您的情况下为" https://mysite.servicebus.windows.net/mysite-notif/registrations/?api-version=2015-01 ")
  • keyName:在您的情况下,默认密钥名称"DefaultFullSharedAccessSignature"
  • expiry:到期时间表示为自1970年1月1日世界00:00:00 UTC以来的秒数.
  • signature-string:使用具有授权规则的PrimaryKey属性的字符串到符号的HMAC-SHA256来计算SAS令牌的签名.字符串到符号由资源URI和到期时间组成,格式如下:
    • StringToSign = <resourceURI> + "\n" + expiry;
    • resourceURI应该是URL-encoded-resourceURI(也是URL编码)
    • 计算StringToSign使用SAS密钥的HMAC-SHA256 (在您的示例中替换为[mykey]).signature-string然后使用URL编码结果.


Dir*_*irk 6

下面是一个的例子预请求脚本对于邮递员产生所需标头:

function getAuthHeader(resourceUri, keyName, key) {

    var d = new Date();
    var sinceEpoch = Math.round(d.getTime() / 1000);

    var expiry = (sinceEpoch + 3600);

    var stringToSign = encodeURIComponent(resourceUri) + '\n' + expiry;

    var hash = CryptoJS.HmacSHA256(stringToSign, key);
    var hashInBase64 = CryptoJS.enc.Base64.stringify(hash);

    var sasToken = 'SharedAccessSignature sr=' + encodeURIComponent(resourceUri) + '&sig=' + encodeURIComponent(hashInBase64) + '&se=' + expiry + '&skn=' + keyName;

    return sasToken;
}

postman.setEnvironmentVariable('azure-authorization', getAuthHeader(request['url'], "mySharedAccessKeyName", "mySharedAccessKey"));
postman.setEnvironmentVariable('current-date',new Date().toUTCString());
Run Code Online (Sandbox Code Playgroud)

要使用它,请执行以下操作:

  1. 将此预请求脚本添加到您的邮递员请求中
  2. 替换mySharedAccessKeyName,mySharedAccessKey与您的凭据
  3. 添加标题 Authorization: {{azure-authorization}}
  4. 添加标题 x-ms-date: {{current-date}}