【发布时间】:2021-03-04 01:12:21
【问题描述】:
我正在尝试通过发出 POST 请求并将 messagettl 指定为 -1 来使用 python3 将消息发布到 azure 队列服务,这表明消息不会过期。在文档https://docs.microsoft.com/en-us/rest/api/storageservices/put-message 中,我必须指定Authorization 键和Date,它们指示响应启动的时间(两个参数都是必需的),并且正文必须是XML,这里我做了什么:
url = "https://MyStorageAccountName.queue.core.windows.net/MyQueueName?messagettl=-1"
xml = """<?xml version='1.0' encoding='utf-8'?>
<QueueMessage>
<MessageText>First message</MessageText>
</QueueMessage> """
headers = {'Content-Type': 'application/xml',
'Authorization' : 'SharedKey MyStorageAccountName:MyKey1....==',
'Date' : str(datetime.utcnow())}
print(requests.post(url, data=xml, headers=headers).text)
而且响应是错误的:
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:44d1fd4c-c003-001d-215...000
Time:2020-11-20T15:39:10.9730253Z</Message>
<AuthenticationErrorDetail>The Date header in the request is incorrect.</AuthenticationErrorDetail>
</Error>
我缺少哪一块拼图?
更新:
在标题中,我通过将str(datetime.utcnow()) 替换为format_date_time(mktime(datetime.now().timetuple())) 来解决了这个问题,并修复了相关的日期错误,但我有一个新错误并且不知道如何签署我的密钥:
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>AuthenticationFailed</Code>
<Message>Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:359305a5-a003-0034...
Time:2020-11-20T15:59:12.4611176Z</Message>
<AuthenticationErrorDetail>The MAC signature found in the HTTP request 'HACSNj/4PwH...MyKey...YJQ==' is not the same as any computed signature. Server used following string to sign: 'POST
application/xml
Fri, 20 Nov 2020 15:59:09 GMT
/MystorageAccount/MyQueueName'.</AuthenticationErrorDetail>
</Error>
【问题讨论】:
标签: python-3.x python-requests azure-queues