通过REST将消息发送到Azure服务总线

编程入门 行业动态 更新时间:2024-10-10 16:24:43
本文介绍了通过REST将消息发送到Azure服务总线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Azure队列公开给REST API.要使REST调用起作用.我在POSTMAN上进行了样本测试.POST呼叫

yournamespace.servicebus.windows/yourentity/messages

此外,在下面的2个标头和值中传递.

标题1:

授权:SharedAccessSignature sr = https%3A%2F%2F.servicebus.windows%2Fyourentity& sig =您上面代码中的签名& se = 1529928563& skn = KeyName

示例:

SharedAccessSignature sr = https%3A%2F%2Fservicebussoatest1.servicebus.windows%2Fpublishque& sig = a0wmRklbCGFCYoSCViij9gagtZV9Bg + vU =& se = 1529928563& skn = testpolicycy

标题2:

Content-Type:应用程序/json

但是,即使我传递了正确的Authorization值,也收到以下错误:

401:无效的授权令牌签名

解决方案

401:无效的授权令牌签名

根据401错误,表示令牌无效.

首先,请确保您的策略有权发送邮件.

第二,如果您想使用

用邮递员对其进行测试.

标题:

Authorization:SharedAccessSignature sr = https%3a%2f%2fyournamespace.servicebus.windows%2fqueuename%2fmessages& sig = SyumAUNnqWFjW2MqjwlomU%2fbblqZljq6LPJp3jpfU =%2b4%内容类型:application/xml

身体

< string xmlns ="schemas.microsoft/2003/10/Serialization/">这是一条消息.

测试结果:

The Azure Queues are exposed to REST API.To make the REST call works. I ran a sample test on POSTMAN. The POST call

yournamespace.servicebus.windows/yourentity/messages

Also, Passing below 2 headers and values.

Header 1:

Authorization: SharedAccessSignature sr=https%3A%2F%2F.servicebus.windows%2Fyourentity&sig=yoursignature from code above&se=1529928563&skn=KeyName

Example:

SharedAccessSignature sr=https%3A%2F%2Fservicebussoatest1.servicebus.windows%2Fpublishque&sig=a0wmRklbCGFCYoSCViij9gagtZV9Bg+vU=&se=1529928563&skn=testpolicy

Header 2:

Content-Type: application/json

But even though I have passed the correct Authorization value, I am getting the error below:

401:Invalid Authorization Token signature

解决方案

401:Invalid Authorization Token signature

According to the 401 error meanings that the token is not vaild.

Firstly please make sure that your policy has access to send the message.

Secondly, if you want to use the azure service bus Send Message Rest APi. The format should be following.

POST <yournamespace>.servicebus.windows/<yourentity>/messages Authorization: SharedAccessSignature sr=https%3A%2F%2F<yournamespace>.servicebus.windows%2F<yourentity>&sig=<yoursignature from code above>&se=1438205742&skn=KeyName ContentType: application/atom+xml;type=entry;charset=utf-8

We also could get more information about Service Bus access control with Shared Access Signatures from this article.

I also do a demo with postman. It works correctly on my side.

I use the following code to get the SAS token.

public static string GetSasToken(string resourceUri, string keyName, string key, TimeSpan ttl) { var expiry = GetExpiry(ttl); string stringToSign = HttpUtility.UrlEncode(resourceUri) + "\n" + expiry; HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)); var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign))); var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}&skn={3}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry, keyName); return sasToken; } private static string GetExpiry(TimeSpan ttl) { TimeSpan expirySinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1) + ttl; return Convert.ToString((int)expirySinceEpoch.TotalSeconds); } string queueUrl = "tomtestsb.servicebus.windows/" + "queue" + "/messages"; string token = GetSasToken(queueUrl,"Key", "value", TimeSpan.FromDays(1));

We could get the key and value with Azure portal

Test it with Postman.

Headers:

Authorization:SharedAccessSignature sr=https%3a%2f%2fyournamespace.servicebus.windows%2fqueuename%2fmessages&sig=SyumAUNnqWFjW2MqjwlomU%2fbblqZljq6LPJp3jpfU%2b4%3d&se=1529478623&skn=KeyName Content-Type:application/xml

Body

<string xmlns="schemas.microsoft/2003/10/Serialization/">This is a message.</string>

Test Result:

更多推荐

通过REST将消息发送到Azure服务总线

本文发布于:2023-11-14 06:11:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586443.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发送到   总线   消息   REST   Azure

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!