@@ -48,13 +48,12 @@ Open Visual Studio Code or other IDE or editor of your choice and create a new f
48
48
Update the ` SignHmacTutorial.py ` script with the following code to begin.
49
49
50
50
``` python
51
- from base64 import b64decode, b64encode
51
+ import base64
52
+ import hashlib
53
+ import hmac
54
+ import json
52
55
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
58
57
```
59
58
60
59
## 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
88
87
89
88
``` python
90
89
def compute_content_hash (content ):
91
- sha_256 = sha256()
90
+ sha_256 = hashlib. sha256()
92
91
sha_256.update(content)
93
92
hashed_bytes = sha_256.digest()
94
- base64_encoded_bytes = b64encode(hashed_bytes)
93
+ base64_encoded_bytes = base64. b64encode(hashed_bytes)
95
94
content_hash = base64_encoded_bytes.decode(' utf-8' )
96
95
return content_hash
97
96
```
@@ -102,14 +101,34 @@ Use the following code to create a method for computing your HMAC signature.
102
101
103
102
``` python
104
103
def compute_signature (string_to_sign , secret ):
105
- decoded_secret = b64decode(secret)
104
+ decoded_secret = base64. b64decode(secret)
106
105
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)
109
108
signature = encoded_signature.decode(' utf-8' )
110
109
return signature
111
110
```
112
111
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
+
113
132
## Create an authorization header string
114
133
115
134
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.
127
146
``` python
128
147
# Specify the 'x-ms-date' header as the current UTC timestamp according to the RFC1123 standard
129
148
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)
132
150
# Compute a content hash for the 'x-ms-content-sha256' header.
133
151
content_hash = compute_content_hash(content)
134
152
@@ -147,9 +165,6 @@ Use the following code to add the required headers.
147
165
``` python
148
166
request_headers = {}
149
167
150
- # Add content type header.
151
- request_headers[" Content-Type" ] = " text/plain; charset=utf-8"
152
-
153
168
# Add a date header.
154
169
request_headers[" x-ms-date" ] = date
155
170
@@ -165,7 +180,8 @@ request_headers["Authorization"] = authorization_header
165
180
Call the endpoint and check the response.
166
181
167
182
``` 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)
170
186
print (response_string)
171
187
```
0 commit comments