【问题标题】:How to implement Direct Batch Send Microsoft Azure API如何实现直接批量发送 Microsoft Azure API
【发布时间】:2016-08-31 09:26:08
【问题描述】:

我正在尝试实现Direct Batch send API 来发送通知。使用 Postman 模拟请求。并返回 Invalid authentication token 错误。

发布请求:

https://mynamespace.servicebus.windows.net/myHub/messages/$batch?direct&api-version=2015-08

带有以下标题:

内容类型:应用程序/json 授权:SharedAccessSignature sr=https://mynamespace.servicebus.windows.net/myHub/messages/$batch?direct%3fapi-version%3d2015-01&sig=xxxx&se=xxxx&skn=DefaultFullSharedAccessSignature

那么如何解决这个错误呢?

还有一个 APNS 示例:

POST https://{Namespace}.servicebus.windows.net/{Notification Hub}/messages/$batch?direct&api-version=2015-08 HTTP/1.1
Content-Type: multipart/mixed; boundary="simple-boundary"
Authorization: SharedAccessSignature sr=https%3a%2f%2f{Namespace}.servicebus.windows.net%2f{Notification Hub}%2fmessages%2f%24batch%3fdirect%26api-version%3d2015-08&sig={Signature}&skn=DefaultFullSharedAccessSignature
ServiceBusNotification-Format: apple
Host: {Namespace}.servicebus.windows.net
Content-Length: 511
Expect: 100-continue
Connection: Keep-Alive


--simple-boundary
Content-Type: application/json
Content-Disposition: inline; name=notification

{"aps":{"alert":"Hello using APNS via Direct Batch Send!!!"}}
--simple-boundary
Content-Type: application/json
Content-Disposition: inline; name=devices

['Device Token1','Device Token2','Device Token3']
--simple-boundary--


--simple-boundary
Content-Type: application/json
Content-Disposition: inline; name=notification

{"aps":{"alert":"Hello using APNS via Direct Batch Send!!!"}}
--simple-boundary
Content-Type: application/json
Content-Disposition: inline; name=devices

['Device Token1','Device Token2','Device Token3']
--simple-boundary--

如何用 Postman 进行测试?

【问题讨论】:

    标签: azure azure-notificationhub


    【解决方案1】:

    您是否尝试过使用 NotificationHubs SDK 调用 Direct Batch Send?如果是,您能否将来自 SDK 的 http 请求与 Postman 发送的请求进行比较?

    关于填写授权标头的文档也包含一些不准确之处。下面的 C# 代码显示了如何以正确的方式进行操作。

    string GenerateSasToken(Uri uri, string sasKeyName, string sasKeyValue)
    {
        var targetUri = HttpUtility.UrlEncode(uri.ToString().ToLower(), Encoding.UTF8).ToLower();
        var expiresOnDate = Convert.ToInt64(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds) + 60*60 /* one hour */;
        var toSign = targetUri + "\n" + expiresOnDate;
    
        var keyBytes = Encoding.UTF8.GetBytes(sasKeyValue);
        var mac = new HMACSHA256(keyBytes);
        mac.Initialize();
        var rawHmac = mac.ComputeHash(Encoding.UTF8.GetBytes(toSign));
        var signature = HttpUtility.UrlEncode(Convert.ToBase64String(rawHmac), Encoding.UTF8);
    
        var token = "SharedAccessSignature sr=" + targetUri + "&sig=" + signature + "&se=" + expiresOnDate + "&skn=" + sasKeyName;
        return token;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-01
      • 2019-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 2011-10-17
      • 2011-12-23
      相关资源
      最近更新 更多