Skip to content

Commit 647f3fc

Browse files
Update imports, use urllib for request, add method to get date in correct format, remove header for content-type
1 parent 9df9028 commit 647f3fc

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

articles/communication-services/tutorials/includes/hmac-header-python.md

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,12 @@ Open Visual Studio Code or other IDE or editor of your choice and create a new f
4848
Update the `SignHmacTutorial.py` script with the following code to begin.
4949

5050
```python
51-
from base64 import b64decode, b64encode
51+
import base64
52+
import hashlib
53+
import hmac
54+
import json
5255
from datetime import datetime, timezone
53-
from hashlib import sha256
54-
from hmac import digest
55-
from json import dumps
56-
from locale import LC_TIME, setlocale
57-
from requests import post
56+
from urllib import request
5857
```
5958

6059
## Prepare data for the request
@@ -88,10 +87,10 @@ The content hash is a part of your HMAC signature. Use the following code to com
8887

8988
```python
9089
def compute_content_hash(content):
91-
sha_256 = sha256()
90+
sha_256 = hashlib.sha256()
9291
sha_256.update(content)
9392
hashed_bytes = sha_256.digest()
94-
base64_encoded_bytes = b64encode(hashed_bytes)
93+
base64_encoded_bytes = base64.b64encode(hashed_bytes)
9594
content_hash = base64_encoded_bytes.decode('utf-8')
9695
return content_hash
9796
```
@@ -102,14 +101,34 @@ Use the following code to create a method for computing your HMAC signature.
102101

103102
```python
104103
def compute_signature(string_to_sign, secret):
105-
decoded_secret = b64decode(secret)
104+
decoded_secret = base64.b64decode(secret)
106105
encoded_string_to_sign = string_to_sign.encode('ascii')
107-
hashed_bytes = digest(decoded_secret, encoded_string_to_sign, digest=sha256)
108-
encoded_signature = b64encode(hashed_bytes)
106+
hashed_bytes = hmac.digest(decoded_secret, encoded_string_to_sign, digest=hashlib.sha256)
107+
encoded_signature = base64.b64encode(hashed_bytes)
109108
signature = encoded_signature.decode('utf-8')
110109
return signature
111110
```
112111

112+
## Get current UTC timestamp according to the RFC1123 standard
113+
114+
Use the following code to get desired date format independent of locale settings.
115+
116+
```python
117+
def format_date(dt):
118+
days = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
119+
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
120+
utc = dt.utctimetuple()
121+
122+
return "{}, {:02} {} {:04} {:02}:{:02}:{:02} GMT".format(
123+
days[utc.tm_wday],
124+
utc.tm_mday,
125+
months[utc.tm_mon-1],
126+
utc.tm_year,
127+
utc.tm_hour,
128+
utc.tm_min,
129+
utc.tm_sec)
130+
```
131+
113132
## Create an authorization header string
114133

115134
We'll now construct the string that we'll add to our authorization header.
@@ -127,8 +146,7 @@ Add the following code to the `SignHmacTutorial.py` script.
127146
```python
128147
# Specify the 'x-ms-date' header as the current UTC timestamp according to the RFC1123 standard
129148
utc_now = datetime.now(timezone.utc)
130-
setlocale(LC_TIME, 'en_US')
131-
date = utc_now.strftime('%a, %d %b %Y %H:%M:%S GMT')
149+
date = format_date(utc_now)
132150
# Compute a content hash for the 'x-ms-content-sha256' header.
133151
content_hash = compute_content_hash(content)
134152

@@ -147,9 +165,6 @@ Use the following code to add the required headers.
147165
```python
148166
request_headers = {}
149167

150-
# Add content type header.
151-
request_headers["Content-Type"] = "text/plain; charset=utf-8"
152-
153168
# Add a date header.
154169
request_headers["x-ms-date"] = date
155170

@@ -165,7 +180,8 @@ request_headers["Authorization"] = authorization_header
165180
Call the endpoint and check the response.
166181

167182
```python
168-
response = post(request_uri, data=content, headers=request_headers)
169-
response_string = response.content.decode("utf-8")
183+
req = request.Request(request_uri, content, request_headers, method='POST')
184+
with request.urlopen(req) as response:
185+
response_string = json.load(response)
170186
print(response_string)
171187
```

0 commit comments

Comments
 (0)