我正在嘗試使用本檔案中提供的 REST API 向服務總線佇列發送訊息: https ://docs.microsoft.com/en-us/rest/api/servicebus/send-message-to-queue
請注意,我不能將 Azure 庫用于此任務,因為我知道 Service Now 不可用,并且我正在 Python 中設定測驗觸發器以模擬將從 Service Now 進行的 REST API 呼叫。
關于存盤佇列,我有一個類似的問題,我嘗試重用相同的解決方案,但是服務總線會回應“缺少授權標頭”這是我在標頭中使用授權的代碼:
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是我從共享訪問策略下的服務總線佇列中獲得的主鍵
se是從現在起 2 年后的紀元時間(w/o mili seconds)
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'}
如果我在標題中沒有 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'}
我不確定如何進行此操作,任何提示和建議將不勝感激。
uj5u.com熱心網友回復:
您收到此錯誤的原因是您錯誤地計算了共享訪問簽名。你可以了解更多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)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/431817.html
標籤:Python 天蓝色 天蓝色服务总线 azure-rest-api
上一篇:每次在靜態HttpClient上清除DefaultRequestHeaders是否會導致AzureFunction應用出現問題?
下一篇:如何參考另一個集合中的資料?
