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
## Add managed identity to your Communication Services solution (Java)
6
+
## Setting Up
7
7
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.
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.
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
50
57
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.
As another example of using managed identities, we'll add this code which uses the same credential to send an SMS:
73
83
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
+
publicSmsSendResult sendSms(String endpoint, String from, String to, String message) {
86
+
SmsClient smsClient =newSmsClientBuilder()
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
75
96
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:
76
98
```java
77
-
publicSendSmsResponse sendSms() {
99
+
publicstaticvoid main(String[] args) {
100
+
App instance =newApp();
78
101
// You can find your endpoint and access key from your resource in the Azure portal
@@ -81,7 +81,7 @@ As another example of using managed identities, we'll add this code which uses t
81
81
82
82
## Write the Main Method
83
83
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:
0 commit comments