You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this tutorial, you'll learn how to sign an HTTP request with an HMAC signature.
20
20
21
+
>[!NOTE]
22
+
>We strongly encourage to use [Azure SDKs](https://github.com/Azure/azure-sdk). Approach described here is a fallback option for cases when Azure SDKs can't be used for any reason.
23
+
21
24
::: zone pivot="programming-language-csharp"
22
25
[!INCLUDE [Sign an HTTP request with C#](./includes/hmac-header-csharp.md)]
Copy file name to clipboardExpand all lines: articles/communication-services/tutorials/includes/hmac-header-python.md
+26-28Lines changed: 26 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,8 +16,8 @@ Before you get started, make sure to:
16
16
17
17
- Create an Azure account with an active subscription. For details, see [Create an account for free](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
18
18
- Download and install [Python](https://www.python.org/).
19
-
- Download and install [Visual Studio Code](https://code.visualstudio.com/).
20
-
- Create an Azure Communication Services resource. For details, see [Create an Azure Communication Services resource](../../quickstarts/create-communication-resource.md). You'll need to record your **resourceEndpoint** and **resourceAccessKey** for this tutorial.
19
+
- Download and install [Visual Studio Code](https://code.visualstudio.com/) or other IDE that supports Python.
20
+
- Create an Azure Communication Services resource. For details, see [Create an Azure Communication Services resource](../../quickstarts/create-communication-resource.md). You'll need your **resource_endpoint_name** and **resource_endpoint_secret** for this tutorial.
21
21
22
22
## Sign an HTTP request with Python
23
23
@@ -48,52 +48,50 @@ Open Visual Studio Code and create a new file named `SignHmacTutorial.py`. Save
48
48
Update the `SignHmacTutorial.py` script with the following code to begin.
49
49
50
50
```python
51
-
import json
52
-
import requests
53
-
import hashlib
54
-
import base64
55
-
import datetime
56
-
import hmac
57
-
from wsgiref.handlers import format_date_time
58
-
from datetime import datetime
59
-
from time import mktime
51
+
from base64 import b64decode, b64encode
52
+
from datetime import datetime, timezone
53
+
from hashlib import sha256
54
+
from hmac import digest
55
+
from json import dumps
56
+
from locale importLC_TIME, setlocale
57
+
from requests import post
60
58
```
61
59
62
60
## Prepare data for the request
63
61
64
-
For this example, we'll sign a request to create a new identity by using the Communication Services Authentication API (version `2021-03-07`).
62
+
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).
65
63
66
64
Add the following code to the `SignHmacTutorial.py` script.
67
65
66
+
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://".
67
+
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.
# Endpoint identities?api-version=2021-03-07 accepts list of scopes as a body.
78
79
scopes = ["chat"]
79
80
80
-
serialized_body =json.dumps(scopes)
81
+
serialized_body = dumps(scopes)
81
82
content = serialized_body.encode("utf-8")
82
83
```
83
84
84
-
Replace `resourceEndpointName` with your real resource endpoint name value.
85
-
Replace `resourceEndpointSecret` with your real resource endpoit secret value.
86
-
87
85
## Create a content hash
88
86
89
87
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.
0 commit comments