Skip to content

Commit 1a97294

Browse files
committed
fixes to js,net. Added Python fixes
1 parent e139883 commit 1a97294

File tree

4 files changed

+112
-24
lines changed

4 files changed

+112
-24
lines changed

articles/communication-services/quickstarts/includes/managed-identity/managed-identity-js.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Open your terminal or command window create a new directory for your app, and navigate to it.
66

77
```console
8-
mkdir managed-identity-quickstart && cd managed-identity--quickstart
8+
mkdir managed-identity-quickstart && cd managed-identity-quickstart
99
```
1010

1111
Run `npm init -y` to create a **package.json** file with default settings.
@@ -44,7 +44,7 @@ We'll be using the [DefaultAzureCredential](/javascript/api/@azure/identity/defa
4444
const credential = new DefaultAzureCredential();
4545
```
4646

47-
## Create an identity and issue a token with Managed Identity
47+
## Create an identity and issue a token with managed identities
4848

4949
Next, we'll write a function which creates a new identity and issues a token for this identity, we'll use this later to test our managed identity setup.
5050

@@ -81,7 +81,7 @@ With our functions created we can now write a main function to call them and dem
8181
async function main() {
8282
// You can find your endpoint and access key from your resource in the Azure portal
8383
// e.g. "https://<RESOURCE_NAME>.communication.azure.com";
84-
endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
84+
const endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
8585

8686

8787
console.log("Retrieving new Access Token, using Managed Identities");
@@ -128,7 +128,7 @@ async function sendSms(resourceEndpoint, fromNumber, toNumber, message) {
128128
async function main() {
129129
// You can find your endpoint and access key from your resource in the Azure portal
130130
// e.g. "https://<RESOURCE_NAME>.communication.azure.com";
131-
endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
131+
const endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
132132

133133

134134
console.log("Retrieving new Access Token, using Managed Identities");

articles/communication-services/quickstarts/includes/managed-identity/managed-identity-net.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ We'll be using the [DefaultAzureCredential](/dotnet/api/azure.identity.defaultaz
4343
private DefaultAzureCredential credential = new DefaultAzureCredential();
4444
```
4545

46-
## Issue a token with Managed Identity
46+
## Issue a token with managed identities
4747

4848
Now we'll add code which uses the created credential, to issue a VoIP Access Token. We'll call this code later on.
4949

@@ -60,9 +60,9 @@ Now we'll add code which uses the created credential, to issue a VoIP Access Tok
6060
}
6161
```
6262

63-
## Send an SMS with Managed Identity
63+
## Send an SMS with managed identities
6464

65-
As another example of using Managed identities, we'll add this code which uses the same credential to send an SMS.
65+
As another example of using managed identities, we'll add this code which uses the same credential to send an SMS;
6666

6767
```csharp
6868
public SmsSendResult SendSms(Uri resourceEndpoint, string from, string to, string message)
Lines changed: 103 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
## Add managed identity to your Communication Services solution
1+
## Setting Up
2+
3+
## Create a new Python application
4+
5+
Open your terminal or command window create a new directory for your app, and navigate to it.
6+
7+
```console
8+
mkdir managed-identity-quickstart && cd managed-identity-quickstart
9+
```
210

311
### Install the SDK packages
412

@@ -8,27 +16,33 @@ pip install azure-communication-identity
816
pip install azure-communication-sms
917
```
1018

19+
### Create a new file
20+
Open and save a new file within your created folder called `managed-identity.py`, we'll be placing our code inside this file.
21+
1122
### Use the SDK packages
1223

13-
Add the following `import` to your code to use the Azure Identity.
24+
Add the following `import` statements to the top of your file to use the SDKs that we installed.
1425

1526
```python
1627
from azure.identity import DefaultAzureCredential
28+
from azure.communication.identity import CommunicationIdentityClient
29+
from azure.communication.sms import SmsClient
1730
```
1831

19-
The examples below are using the [DefaultAzureCredential](/python/api/azure-identity/azure.identity.defaultazurecredential). This credential is suitable for production and development environments.
32+
### Create a DefaultAzureCredential
2033

21-
To register application in the development environment and set up environment variables, see [Authorize access with managed identity](../managed-identity-from-cli.md)
34+
We'll be using the [DefaultAzureCredential](/python/api/azure-identity/azure.identity.defaultazurecredential). This credential is suitable for production and development environments. As we'll be using it throughout this quickstart we'll create it at the top of the file.
2235

23-
### Create an identity and issue a token
36+
```python
37+
credential = DefaultAzureCredential()
38+
```
2439

25-
The following code example shows how to create a service client object with managed identity, then use the client to issue a token for a new user:
40+
## Create an identity and issue a token with managed identities.
2641

27-
```python
28-
from azure.communication.identity import CommunicationIdentityClient
42+
Now we'll add code which uses the created credential, to issue a VoIP Access Token. We'll call this code later on:
2943

44+
```python
3045
def create_identity_and_get_token(resource_endpoint):
31-
credential = DefaultAzureCredential()
3246
client = CommunicationIdentityClient(resource_endpoint, credential)
3347

3448
user = client.create_user()
@@ -37,14 +51,11 @@ def create_identity_and_get_token(resource_endpoint):
3751
return token_response
3852
```
3953

40-
### Send an SMS with Azure managed identity
41-
The following code example shows how to create a service client object with Azure managed identity, then use the client to send an SMS message:
54+
### Send an SMS with managed identities
55+
As another example of using managed identities, we'll add this code which uses the same credential to send an SMS:
4256

4357
```python
44-
from azure.communication.sms import SmsClient
45-
4658
def send_sms(resource_endpoint, from_phone_number, to_phone_number, message_content):
47-
credential = DefaultAzureCredential()
4859
sms_client = SmsClient(resource_endpoint, credential)
4960

5061
sms_client.send(
@@ -53,4 +64,81 @@ def send_sms(resource_endpoint, from_phone_number, to_phone_number, message_cont
5364
message=message_content,
5465
enable_delivery_report=True # optional property
5566
)
56-
```
67+
```
68+
69+
## Write our main code
70+
71+
With our functions created we can now write the main code which will call the functions we've previous written.
72+
73+
```python
74+
# You can find your endpoint and access key from your resource in the Azure portal
75+
# e.g. "https://<RESOURCE_NAME>.communication.azure.com";
76+
endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
77+
78+
print("Retrieving new Access Token, using Managed Identities");
79+
result = create_identity_and_get_token(endpoint);
80+
print(f'Retrieved Access Token: {result.token}');
81+
82+
print("Sending SMS using Managed Identities");
83+
84+
# You will need a phone number from your resource to send an SMS.
85+
sms_result = send_sms(endpoint, "<FROM_NUMBER>", "<TO_NUMBER>", "Hello from Managed Identities");
86+
print(f'SMS ID: {sms_result[0].message_id}');
87+
print(f'Send Result Successful: {sms_result[0].successful}');
88+
```
89+
90+
The final `managed-identity.py` file should look something like this:
91+
92+
```python
93+
from azure.identity import DefaultAzureCredential
94+
from azure.communication.identity import CommunicationIdentityClient
95+
from azure.communication.sms import SmsClient
96+
97+
credential = DefaultAzureCredential()
98+
99+
def create_identity_and_get_token(resource_endpoint):
100+
client = CommunicationIdentityClient(resource_endpoint, credential)
101+
102+
user = client.create_user()
103+
token_response = client.get_token(user, scopes=["voip"])
104+
105+
return token_response
106+
107+
def send_sms(resource_endpoint, from_phone_number, to_phone_number, message_content):
108+
sms_client = SmsClient(resource_endpoint, credential)
109+
110+
response = sms_client.send(
111+
from_=from_phone_number,
112+
to=[to_phone_number],
113+
message=message_content,
114+
enable_delivery_report=True # optional property
115+
)
116+
return response
117+
118+
# You can find your endpoint and access key from your resource in the Azure portal
119+
# e.g. "https://<RESOURCE_NAME>.communication.azure.com";
120+
endpoint = "https://<RESOURCE_NAME>.communication.azure.com/"
121+
122+
print("Retrieving new Access Token, using Managed Identities");
123+
result = create_identity_and_get_token(endpoint);
124+
print(f'Retrieved Access Token: {result.token}');
125+
126+
print("Sending SMS using Managed Identities");
127+
128+
# You will need a phone number from your resource to send an SMS.
129+
sms_result = send_sms(endpoint, "<FROM_NUMBER>", "<TO_NUMBER>", "Hello from Managed Identities");
130+
print(f'SMS ID: {sms_result[0].message_id}');
131+
print(f'Send Result Successful: {sms_result[0].successful}');
132+
```
133+
## Run the Program
134+
135+
With everything complete, you can run the file by entering `python managed-identity.py` from your project's directory. If everything went well you should see something similar to the following.
136+
137+
```Bash
138+
$ python managed-identity.py
139+
Retrieving new Access Token, using Managed Identities
140+
Retrieved Access Token: ey...Q
141+
Sending SMS using Managed Identities
142+
SMS ID: Outgoing_2021040602194...._noam
143+
Send Result Successful: true
144+
```

articles/communication-services/quickstarts/managed-identity.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ This quickstart shows you how to authorize access to the Identity and SMS SDKs f
2020
## Prerequisites
2121

2222
- An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free)
23-
- An active Azure Communication Services resource, see [create a Communication Services resource](../create-communication-resource.md) if you do not have one.
23+
- An active Azure Communication Services resource, see [create a Communication Services resource](./create-communication-resource.md) if you do not have one.
2424
- To send an SMS you will need a [Phone Number](./telephony-sms/get-phone-number.md).
25-
- A setup managed identity for a development environment, see [Authorize access with managed identity](../managed-identity-from-cli.md)
25+
- A setup managed identity for a development environment, see [Authorize access with managed identity](./managed-identity-from-cli.md)
2626

2727
::: zone pivot="programming-language-csharp"
2828
[!INCLUDE [.NET](./includes/managed-identity/managed-identity-net.md)]

0 commit comments

Comments
 (0)