如何使用 SAS 对服务总线的 Azure REST API 进行身份验证

How to authenticate Azure REST APIs to Service Bus with SAS

我正在尝试使用本文档中提供的 REST API 向服务总线 queue 发送消息: https://docs.microsoft.com/en-us/rest/api/servicebus/send-message-to-queue

请注意,我不能使用 Azure 库执行此任务,因为我知道现在服务不可用,我正在 Python 中设置测试触发器以模拟 REST API 将从 Service Now 发出的调用。

我有一个关于 的类似问题,我尝试重复使用相同的解决方案,但是服务总线会以“缺少授权 Header”作为响应 这是我在 header:

中使用授权的代码
import requests
api = f"https://{service_namespace}.servicebus.windows.net/{queue}/messages?"
msg = """<QueueMessage>  
<MessageText>Testing 1234</MessageText>  
</QueueMessage>
"""
header = {
    "Authorization": f"SharedAccessSignature sr=https://{service_namespace}.servicebus.windows.net/{queue}&sig={sig}&se={se}&skn={skn}",
    "Content-Type": "application/atom+xml;type=entry;charset=utf-8"
}
resp = requests.post(api, data=msg, headers=header)
print(resp)
print(resp.text)
print(resp.headers)

这里,sig 是我从共享访问策略

下的服务总线 Queue 获得的主键

se 是 2 年后的纪元时间(w/o mili 秒) skn 是策略的名称

我得到的最终回复是

<Response [401]>

{'Content-Length': '0', 'Server': 'Microsoft-HTTPAPI/2.0', 'Strict-Transport-Security': 'max-age=31536000', 'Date': 'Thu, 24 Feb 2022 09:27:17 GMT'}

如果我 post 在 header 中没有 Auth 并使用上面突出显示的问题中的解决方案, 这是 API 结构顺便说一句: f"https://{service_namespace}.servicebus.windows.net/{queue}/messages?sig={sig}&se={se}&skn={skn}" 我收到此错误:

<Error><Code>401</Code><Detail>MissingToken: The authorization header was not found. To know more visit https://aka.ms/sbResourceMgrExceptions. . TrackingId:<redacted>, SystemTracker:<redacted>.servicebus.windows.net:<redacted>/messages, Timestamp:2022-02-24T09:31:09</Detail></Error>
{'Transfer-Encoding': 'chunked', 'Content-Type': 'application/xml; charset=utf-8', 'Server': 'Microsoft-HTTPAPI/2.0', 'Strict-Transport-Security': 'max-age=31536000', 'Date': 'Thu, 24 Feb 2022 09:31:09 GMT'}

我不确定如何进行,如有任何提示和建议,我们将不胜感激。

您收到此错误的原因是您错误地计算了共享访问签名。您可以了解更多 here.

要使用 python 生成 SAS 令牌,请参阅下面摘自 here:

的代码
import time
import urllib
import hmac
import hashlib
import base64

def get_auth_token(sb_name, eh_name, sas_name, sas_value):
    """
    Returns an authorization token dictionary 
    for making calls to Event Hubs REST API.
    """
    uri = urllib.parse.quote_plus("https://{}.servicebus.windows.net/{}" \
                                  .format(sb_name, eh_name))
    sas = sas_value.encode('utf-8')
    expiry = str(int(time.time() + 10000))
    string_to_sign = (uri + '\n' + expiry).encode('utf-8')
    signed_hmac_sha256 = hmac.HMAC(sas, string_to_sign, hashlib.sha256)
    signature = urllib.parse.quote(base64.b64encode(signed_hmac_sha256.digest()))
    return  {"sb_name": sb_name,
             "eh_name": eh_name,
             "token":'SharedAccessSignature sr={}&sig={}&se={}&skn={}' \
                     .format(uri, signature, expiry, sas_name)
            }