Skip to content

Commit 862dd90

Browse files
committed
review comments
1 parent e219578 commit 862dd90

File tree

1 file changed

+97
-2
lines changed

1 file changed

+97
-2
lines changed

articles/communication-services/quickstarts/advanced-messaging/whatsapp/includes/common-setting-python.md

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,104 @@ zone_pivot_groups: acs-js-csharp-java-python
1515

1616
### Authenticate the client
1717

18-
The Messages SDK uses the `NotificationMessagesClient` to send messages. The `NotificationMessagesClient` method authenticates using your connection string acquired from Azure Communication Services resource in the Azure portal. For more information about connection strings, see [access-your-connection-strings-and-service-endpoints](../../../create-communication-resource.md#access-your-connection-strings-and-service-endpoints).
18+
Messages sending is done using NotificationMessagesClient. NotificationMessagesClient is authenticated using your connection string acquired from Azure Communication Services resource in the Azure portal. For more information on connection strings, see [access-your-connection-strings-and-service-endpoints](../../../../create-communication-resource.md#access-your-connection-strings-and-service-endpoints).
1919

20-
[!INCLUDE [Authenticate the client ](./authenticate-notification-messages-client-net.md)]
20+
#### [Connection String](#tab/connection-string)
21+
22+
Get Azure Communication Resource connection string from Azure portal as given in screenshot. On the left, navigate to the `Keys` tab. Copy the `Connection string` field for the primary key. The connection string is in the format `endpoint=https://{your Azure Communication Services resource name}.communication.azure.com/;accesskey={secret key}`.
23+
24+
:::image type="content" source="../../media/get-started/get-communication-resource-connection-string.png" alt-text="Screenshot that shows an Azure Communication Services resource in the Azure portal, viewing the 'Primary Key' field in the 'Keys' section.":::
25+
26+
Set the environment variable `COMMUNICATION_SERVICES_CONNECTION_STRING` to the value of your connection string.
27+
Open a console window and enter the following command:
28+
```console
29+
setx COMMUNICATION_SERVICES_CONNECTION_STRING "<your connection string>"
30+
```
31+
After you add the environment variable, you might need to restart any running programs that will need to read the environment variable, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example.
32+
33+
For more information on how to set an environment variable for your system, follow the steps at [Store your connection string in an environment variable](../../../../create-communication-resource.md#store-your-connection-string-in-an-environment-variable).
34+
35+
```python
36+
# Get a connection string to our Azure Communication Services resource.
37+
connection_string = os.getenv("COMMUNICATION_SERVICES_CONNECTION_STRING")
38+
39+
def send_template_message(self):
40+
from azure.communication.messages import NotificationMessagesClient
41+
42+
# Create NotificationMessagesClient Client
43+
messaging_client = NotificationMessagesClient.from_connection_string(self.connection_string)
44+
```
45+
46+
#### [Microsoft Entra ID](#tab/aad)
47+
48+
NotificationMessagesClient is also authenticated using Microsoft Entra ID/TokenCredentials. For more information, see [access-Azure-Communication-Resources-using-TokenCredentials](/python/api/overview/azure/identity-readme?view=azure-python&preserve-view=true#environment-variables).
49+
50+
The [`azure.identity`](https://github.com/Azure/azure-sdk-for-python/tree/azure-identity_1.15.0/sdk/identity/azure-identity) package provides various credential types that your application can use to authenticate. You can choose from the various options to authenticate the identity client detailed at [Azure Identity - Credential providers](/python/api/overview/azure/identity-readme?view=azure-python#credential-classes&preserve-view=true) and [Azure Identity - Authenticate the client](/python/api/overview/azure/identity-readme?view=azure-python#authenticate-with-defaultazurecredential&preserve-view=true). This option walks through one way of using the [`DefaultAzureCredential`](/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential&preserve-view=true).
51+
52+
The `DefaultAzureCredential` attempts to authenticate via [`several mechanisms`](/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential&preserve-view=true) and it might be able to find its authentication credentials if you're signed into Visual Studio or Azure CLI. However, this option walks you through setting up with environment variables.
53+
54+
To create a `DefaultAzureCredential` object:
55+
1. To set up your service principle app, follow the instructions at [Creating a Microsoft Entra registered Application](../../../../identity/service-principal.md?pivots=platform-azcli#creating-a-microsoft-entra-registered-application).
56+
57+
1. Set the environment variables `AZURE_CLIENT_ID`, `AZURE_CLIENT_SECRET`, and `AZURE_TENANT_ID` using the output of your app's creation.
58+
Open a console window and enter the following commands:
59+
```console
60+
setx COMMUNICATION_SERVICES_ENDPOINT_STRING "<https://<resource name>.communication.azure.com>"
61+
setx AZURE_CLIENT_ID "<your app's appId>"
62+
setx AZURE_CLIENT_SECRET "<your app's password>"
63+
setx AZURE_TENANT_ID "<your app's tenant>"
64+
```
65+
After you add the environment variables, you might need to restart any running programs that will need to read the environment variables, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example.
66+
67+
1. To use the [`DefaultAzureCredential`](/python/api/overview/azure/identity-readme?view=azure-python#defaultazurecredential&preserve-view=true) provider, or other credential providers provided with the Azure SDK, install the `azure.identity` python package and then instantiate client.
68+
69+
```python
70+
# Get a connection string to our Azure Communication Services resource.
71+
endpoint_string = os.getenv("COMMUNICATION_SERVICES_ENDPOINT_STRING")
72+
73+
def send_template_message(self):
74+
from azure.communication.messages import NotificationMessagesClient
75+
from azure.identity import DefaultAzureCredential
76+
77+
# Create NotificationMessagesClient Client
78+
messaging_client = NotificationMessagesClient(endpoint=self.endpoint_string,
79+
credential=DefaultAzureCredential())
80+
```
81+
82+
#### [AzureKeyCredential](#tab/azurekeycredential)
83+
84+
You can also authenticate with an AzureKeyCredential.
85+
86+
Get the endpoint and key from your Azure Communication Services resource in the Azure portal. On the left, navigate to the `Keys` tab. Copy the `Endpoint` and the `Key` field for the primary key.
87+
88+
:::image type="content" source="../../media/get-started/get-communication-resource-endpoint-and-key.png" lightbox="../../media/get-started/get-communication-resource-endpoint-and-key.png" alt-text="Screenshot that shows an Azure Communication Services resource in the Azure portal, viewing the 'Connection string' field in the 'Primary key' section.":::
89+
90+
Set the environment variable `COMMUNICATION_SERVICES_KEY` to the value of your connection string.
91+
Open a console window and enter the following command:
92+
```console
93+
setx COMMUNICATION_SERVICES_ENDPOINT_STRING "<https://<resource name>.communication.azure.com>"
94+
setx COMMUNICATION_SERVICES_KEY "<your key>"
95+
```
96+
After you add the environment variable, you might need to restart any running programs that will need to read the environment variable, including the console window. For example, if you're using Visual Studio as your editor, restart Visual Studio before running the example.
97+
98+
For more information on how to set an environment variable for your system, follow the steps at [Store your connection string in an environment variable](../../../../create-communication-resource.md#store-your-connection-string-in-an-environment-variable).
99+
100+
To instantiate a `NotificationMessagesClient`, add the following code:
101+
102+
```python
103+
# Get a connection string to our Azure Communication Services resource.
104+
endpoint_string = os.getenv("COMMUNICATION_SERVICES_ENDPOINT_STRING")
105+
key = os.getenv("COMMUNICATION_SERVICES_KEY")
106+
107+
def send_template_message(self):
108+
from azure.core.credentials import AzureKeyCredential
109+
from azure.communication.messages import NotificationMessagesClient
110+
111+
# Create NotificationMessagesClient Client
112+
messaging_client = NotificationMessagesClient(endpoint=self.endpoint_string,
113+
credential=AzureKeyCredential(self.key))
114+
```
115+
---
21116

22117
### Set channel registration ID
23118

0 commit comments

Comments
 (0)