Skip to content

Commit 1ab8c5d

Browse files
committed
fixup .net and java
1 parent 1a97294 commit 1ab8c5d

File tree

2 files changed

+141
-35
lines changed

2 files changed

+141
-35
lines changed

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

Lines changed: 139 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,21 @@ For Java, you'll also need:
33
- [Java Development Kit (JDK)](https://docs.microsoft.com/azure/developer/java/fundamentals/java-jdk-install) version 8 or above.
44
- [Apache Maven](https://maven.apache.org/download.cgi).
55

6-
## Add managed identity to your Communication Services solution (Java)
6+
## Setting Up
77

8-
### Install the SDK packages
9-
In the pom.xml file, add the following dependency elements to the group of dependencies.
8+
### Create a new Java application
9+
10+
Open your terminal or command window. Navigate to the directory where you'd like to create your Java application. Run the command below to generate the Java project from the maven-archetype-quickstart template.
11+
12+
```console
13+
mvn archetype:generate -DgroupId=com.communication.quickstart -DartifactId=communication-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
14+
```
15+
16+
You'll notice that the 'generate' task created a directory with the same name as the `artifactId`. Under this directory, the src/main/java directory contains the project source code, the `src/test/java directory` contains the test source, and the `pom.xml` file is the project's Project Object Model, or POM.
17+
18+
### Install the package
19+
20+
Open the **pom.xml** file in your text editor. Add the following dependency element to the group of dependencies.
1021

1122
```xml
1223
<dependency>
@@ -35,63 +46,158 @@ import com.azure.communication.common.*;
3546
import com.azure.communication.identity.*;
3647
import com.azure.communication.identity.models.*;
3748
import com.azure.communication.sms.*;
49+
import com.azure.communication.sms.models.*;
3850
import com.azure.core.credential.*;
3951
import com.azure.identity.*;
4052

41-
import java.io.IOException;
4253
import java.util.*;
4354
```
4455

45-
The examples below are using the [DefaultAzureCredential](/java/api/com.azure.identity.defaultazurecredential). This credential is suitable for production and development environments.
46-
47-
`AZURE_CLIENT_SECRET`, `AZURE_CLIENT_ID` and `AZURE_TENANT_ID` environment variables are needed to create a `DefaultAzureCredential` object. To create a registered application in the development environment and set up environment variables, see [Authorize access with managed identity](../managed-identity-from-cli.md).
48-
49-
### Create an identity and issue a token with Managed Identity
56+
## Create a DefaultAzureCredential
5057

51-
The following code example shows how to create a service client object with managed identity.
52-
Then, use the client to issue a token for a new user:
58+
We'll be using the [DefaultAzureCredential](/java/api/com.azure.identity.defaultazurecredential) for this quickstart. This credential is suitable for production and development environments. As it is needed for each operation let's create it within the `App.java` class. Add the following to the top of the `App.java` class.
5359

5460
```java
55-
public AccessToken createIdentityAndGetTokenAsync() {
56-
// You can find your endpoint and access key from your resource in the Azure portal
57-
String endpoint = "https://<RESOURCE_NAME>.communication.azure.com";
61+
private TokenCredential credential = new DefaultAzureCredentialBuilder().build();
62+
```
5863

59-
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
64+
## Issue a token with managed identities
6065

66+
Now we'll add code which uses the created credential, to issue a VoIP Access Token. We'll call this code later on;
67+
68+
```java
69+
public AccessToken createIdentityAndGetTokenAsync(String endpoint) {
6170
CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder()
62-
.endpoint(endpoint)
63-
.credential(credential)
64-
.buildClient();
71+
.endpoint(endpoint)
72+
.credential(this.credential)
73+
.buildClient();
6574

6675
CommunicationUserIdentifier user = communicationIdentityClient.createUser();
67-
AccessToken userToken = communicationIdentityClient.getToken(user, new ArrayList<>(Arrays.asList(CommunicationTokenScope.CHAT)));
68-
return userToken;
69-
}
76+
return communicationIdentityClient.getToken(user, new ArrayList<>(Arrays.asList(CommunicationTokenScope.CHAT)));
77+
}
7078
```
7179

72-
### Send an SMS with Managed Identity
80+
## Send an SMS with managed identities
81+
82+
As another example of using managed identities, we'll add this code which uses the same credential to send an SMS:
7383

74-
The following code example shows how to create a service client object with managed identity, then use the client to send an SMS message:
84+
```java
85+
public SmsSendResult sendSms(String endpoint, String from, String to, String message) {
86+
SmsClient smsClient = new SmsClientBuilder()
87+
.endpoint(endpoint)
88+
.credential(this.credential)
89+
.buildClient();
90+
91+
// Send the message and check the response for a message id
92+
return smsClient.send(from, to, message);
93+
}
94+
```
95+
## Write the Main Method
7596

97+
Your `App.java` should already have a Main method, let's add some code which will call our previously created code to demonstrate the use of managed identities:
7698
```java
77-
public SendSmsResponse sendSms() {
99+
public static void main(String[] args) {
100+
App instance = new App();
78101
// You can find your endpoint and access key from your resource in the Azure portal
79-
String endpoint = "https://<RESOURCE_NAME>.communication.azure.com";
102+
// e.g. "https://<RESOURCE_NAME>.communication.azure.com";
103+
String endpoint = "https://<RESOURCE_NAME>.communication.azure.com/";
104+
105+
System.out.println("Retrieving new Access Token, using Managed Identities");
106+
AccessToken token = instance.createIdentityAndGetTokenAsync(endpoint);
107+
System.out.println("Retrieved Access Token: "+ token.getToken());
108+
109+
System.out.println("Sending SMS using Managed Identities");
110+
// You will need a phone number from your resource to send an SMS.
111+
SmsSendResult result = instance.sendSms(endpoint, "<FROM NUMBER>", "<TO NUMBER>", "Hello from Managed Identities");
112+
System.out.println("Sms id: "+ result.getMessageId());
113+
System.out.println("Send Result Successful: "+ result.isSuccessful());
114+
}
115+
```
116+
117+
Your final `App.java` should look like this:
118+
119+
```java
120+
package com.communication.quickstart;
121+
122+
import com.azure.communication.common.*;
123+
import com.azure.communication.identity.*;
124+
import com.azure.communication.identity.models.*;
125+
import com.azure.communication.sms.*;
126+
import com.azure.communication.sms.models.*;
127+
import com.azure.core.credential.*;
128+
import com.azure.identity.*;
129+
130+
import java.util.*;
80131

81-
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
132+
public class App
133+
{
82134

135+
private TokenCredential credential = new DefaultAzureCredentialBuilder().build();
136+
137+
public SmsSendResult sendSms(String endpoint, String from, String to, String message) {
83138
SmsClient smsClient = new SmsClientBuilder()
84139
.endpoint(endpoint)
85-
.credential(credential)
140+
.credential(this.credential)
86141
.buildClient();
87142

88143
// Send the message and check the response for a message id
89-
SmsSendResult response = smsClient.send(
90-
"<from-phone-number>",
91-
"<to-phone-number>",
92-
"your message"
93-
);
94-
return response;
144+
return smsClient.send(from, to, message);
95145
}
146+
public AccessToken createIdentityAndGetTokenAsync(String endpoint) {
147+
CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder()
148+
.endpoint(endpoint)
149+
.credential(this.credential)
150+
.buildClient();
151+
152+
CommunicationUserIdentifier user = communicationIdentityClient.createUser();
153+
return communicationIdentityClient.getToken(user, new ArrayList<>(Arrays.asList(CommunicationTokenScope.CHAT)));
154+
}
155+
156+
public static void main(String[] args) {
157+
App instance = new App();
158+
// You can find your endpoint and access key from your resource in the Azure portal
159+
// e.g. "https://<RESOURCE_NAME>.communication.azure.com";
160+
String endpoint = "https://<RESOURCE_NAME>.communication.azure.com/";
161+
162+
System.out.println("Retrieving new Access Token, using Managed Identities");
163+
AccessToken token = instance.createIdentityAndGetTokenAsync(endpoint);
164+
System.out.println("Retrieved Access Token: "+ token.getToken());
165+
166+
System.out.println("Sending SMS using Managed Identities");
167+
// You will need a phone number from your resource to send an SMS.
168+
SmsSendResult result = instance.sendSms(endpoint, "<FROM NUMBER>", "<TO NUMBER>", "Hello from Managed Identities");
169+
System.out.println("Sms id: "+ result.getMessageId());
170+
System.out.println("Send Result Successful: "+ result.isSuccessful());
171+
}
172+
}
173+
```
174+
175+
## Run the code
176+
177+
Navigate to the directory containing the *pom.xml* file and compile the project by using the following `mvn` command.
178+
179+
```console
180+
mvn compile
181+
```
182+
183+
Then, build the package.
184+
185+
```console
186+
mvn package
187+
```
188+
189+
Run the following `mvn` command to execute the app.
190+
191+
```console
192+
mvn exec:java -Dexec.mainClass="com.communication.quickstart.App" -Dexec.cleanupDaemonThreads=false
193+
```
194+
195+
The final output should resemble the following:
196+
```
197+
Retrieving new Access Token, using Managed Identities
198+
Retrieved Access Token: ey..A
199+
Sending SMS using Managed Identities
200+
Sms id: Outgoing_202104...33f8ae1f_noam
201+
Send Result Successful: true
96202
```
97203

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Now we'll add code which uses the created credential, to issue a VoIP Access Tok
6262

6363
## 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)
@@ -81,7 +81,7 @@ As another example of using managed identities, we'll add this code which uses t
8181

8282
## Write the Main Method
8383

84-
Your `Program.cs` should already have a Main method, let's add some code which will call our previously created code to demonstrate the use of Managed Identities:
84+
Your `Program.cs` should already have a Main method, let's add some code which will call our previously created code to demonstrate the use of managed identities:
8585

8686
```csharp
8787
static void Main(string[] args)

0 commit comments

Comments
 (0)