Skip to content

Commit 9e01c31

Browse files
Merge pull request #214272 from maximrytych-ms/feature/maximrytych/fix-python-hmac-header-sample
Fix Python HMAC sample
2 parents cbdadd1 + 0645de6 commit 9e01c31

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

articles/communication-services/tutorials/hmac-header-tutorial.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: Learn how to sign an HTTP request with C#
2+
title: Learn how to sign an HTTP request with HMAC
33
titleSuffix: An Azure Communication Services tutorial
4-
description: Learn how to sign an HTTP request for Azure Communication Services via C#.
4+
description: Learn how to sign an HTTP request for Azure Communication Services using HMAC.
55
author: alexandra142
66
manager: soricos
77
services: azure-communication-services

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ The following steps describe how to construct the authorization header.
4141

4242
### Create a new Python script
4343

44-
Open Visual Studio Code or other IDE or editor of your choice and create a new file named `SignHmacTutorial.py`. Save this file to a known folder.
44+
Open Visual Studio Code or other IDE or editor of your choice and create a new file named `sign_hmac_tutorial.py`. Save this file to a known folder.
4545

4646
## Add necessary imports
4747

48-
Update the `SignHmacTutorial.py` script with the following code to begin.
48+
Update the `sign_hmac_tutorial.py` script with the following code to begin.
4949

5050
```python
5151
import base64
@@ -60,7 +60,7 @@ from urllib import request
6060

6161
For this example, we'll sign a request to create a new identity by using the Communication Services Authentication API [(version `2021-03-07`)](https://github.com/Azure/azure-rest-api-specs/tree/main/specification/communication/data-plane/Identity/stable/2021-03-07).
6262

63-
Add the following code to the `SignHmacTutorial.py` script.
63+
Add the following code to the `sign_hmac_tutorial.py` script.
6464

6565
- Replace `resource_endpoint_name` with your real resource endpoint name value. This value can be found in Overview section of your Azure Communication Services resource. It's the value of "Endpoint" after "https://".
6666
- Replace `resource_endpoint_secret` with your real resource endpoint secret value. This value can be found in Keys section of your Azure Communication Services resource. It's the value of "Key" - either primary or secondary.
@@ -75,15 +75,15 @@ secret = "resource_endpoint_secret"
7575
request_uri = f"{resource_endpoint}{path_and_query}"
7676

7777
# Endpoint identities?api-version=2021-03-07 accepts list of scopes as a body.
78-
scopes = ["chat"]
78+
body = { "createTokenWithScopes": ["chat"] }
7979

80-
serialized_body = json.dumps(scopes)
80+
serialized_body = json.dumps(body)
8181
content = serialized_body.encode("utf-8")
8282
```
8383

8484
## Create a content hash
8585

86-
The content hash is a part of your HMAC signature. Use the following code to compute the content hash. You can add this method to `SignHmacTutorial.py` script.
86+
The content hash is a part of your HMAC signature. Use the following code to compute the content hash. You can add this method to `sign_hmac_tutorial.py` script.
8787

8888
```python
8989
def compute_content_hash(content):
@@ -141,7 +141,7 @@ We'll now construct the string that we'll add to our authorization header.
141141
1. Compute the signature.
142142
1. Concatenate the string, which will be used in the authorization header.
143143

144-
Add the following code to the `SignHmacTutorial.py` script.
144+
Add the following code to the `sign_hmac_tutorial.py` script.
145145

146146
```python
147147
# Specify the 'x-ms-date' header as the current UTC timestamp according to the RFC1123 standard
@@ -173,6 +173,9 @@ request_headers["x-ms-content-sha256"] = content_hash
173173

174174
# Add authorization header.
175175
request_headers["Authorization"] = authorization_header
176+
177+
# Add content type header.
178+
request_headers["Content-Type"] = "application/json"
176179
```
177180

178181
## Test the client

0 commit comments

Comments
 (0)