【问题标题】:send msg to Azure service bus que via REST通过 REST 将 msg 发送到 Azure 服务总线队列
【发布时间】:2018-06-18 17:47:45
【问题描述】:

Azure 队列向 REST API 公开。以使 REST 调用正常工作。我在 POSTMAN 上进行了示例测试。 POST 调用

https://yournamespace.servicebus.windows.net/yourentity/messages

另外,传递以下 2 个标题和值。

标题 1:

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

例子:

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

标题 2:

Content-Type: application/json

但即使我传递了正确的授权值,我仍然收到以下错误:

401:无效的授权令牌签名

【问题讨论】:

    标签: rest azure azure-servicebus-queues


    【解决方案1】:

    401:无效的授权令牌签名

    根据 401 错误意思是令牌无效。

    首先请确保您的策略有权发送消息。

    其次,如果你想使用azure service bus Send Message Rest APi。格式应如下。

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

    我们还可以获得有关共享访问的服务总线访问控制的更多信息 来自this article的签名。

    我还用邮递员做了一个演示。它在我这边工作正常。

    我使用以下代码获取 SAS 令牌。

    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 = "https://tomtestsb.servicebus.windows.net/" + "queue" + "/messages";
    string token = GetSasToken(queueUrl,"Key", "value", TimeSpan.FromDays(1));
    

    我们可以通过 Azure 门户获取密钥和值

    用 Postman 测试一下。

    标题:

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

    身体

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

    测试结果:

    【讨论】:

    • 要从服务总线队列接收消息,我们将在 REST api 上执行 DELETE 操作(从队列接收和删除消息)。我的问题是,我们如何才能频繁地从队列中收听消息。尽快放入队列中的消息,我们的 REST 服务应该获取消息,而不是调用 REST 调用。
    • 如果可以使用 Azure 功能,我建议您可以使用Azure function with service bus binding 来执行此操作。
    • 注意:当我尝试在队列上使用共享访问策略时,它会因授权令牌错误而失败,但是如果我在服务总线命名空间上使用共享访问策略,它就可以工作。
    • 当你说Firstly please make sure that your policy has access to send the message时,你能详细说明你的意思吗?
    • 很好的答案,为我工作!如果您想要非常持久的令牌(对我来说:100 年),请替换 return Convert.ToString((int)expirySinceEpoch.TotalSeconds); with return Convert.ToString((long)expirySinceEpoch.TotalSeconds);
    【解决方案2】:

    这对我有用:

    POST 到的网址: https://[ServiceBusNamespace].servicebus.windows.net/[QueueName]/messages

    授权:使用 Tom Sun - MSFT 提供的代码

    内容类型:应用程序/json

    【讨论】:

    • 不确定这个答案如何在已经说过的内容中添加任何内容
    猜你喜欢
    • 2022-12-19
    • 2012-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多