0

I'm trying to post a message to azure queue service using python3 by making a POST request and specifying messagettl to -1 which indicates the message does not expire. In the doc https://learn.microsoft.com/en-us/rest/api/storageservices/put-message I have to specify the Authorization key and Date which indicates the time at which the response was initiated (both parameters are required), and the body must be an XML, here what I did:

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)

And the response is an error:

<?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>

which piece of the puzzle I am missing?

UPDATE:

In headers I fixed the issue by replacing str(datetime.utcnow()) with format_date_time(mktime(datetime.now().timetuple())) and fixed the related date error, but I have a new error and don't know how to sign my key:

<?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>
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
marOne
  • 129
  • 2
  • 13

1 Answers1

2

I think using python SDK to do this is much easier, just try the code below:

from azure.storage.queue import QueueClient

connectionString = "<storage account connection string>"
queueName = "<queue name>"

queueClient = QueueClient.from_connection_string(connectionString, queueName)

queueClient.send_message(content = 'hello sdk', time_to_live=-1)

Result:

enter image description here

For info about python queue client sdk, just refer to this doc.

Stanley Gong
  • 11,522
  • 1
  • 8
  • 16