@@ -17,6 +17,9 @@ ms.author: gistefan
17
17
- [ Java Development Kit (JDK)] ( /azure/developer/java/fundamentals/java-jdk-install ) version 8 or above.
18
18
- [ Apache Maven] ( https://maven.apache.org/download.cgi ) .
19
19
20
+ ## Final code
21
+ Find the finalized code for this quickstart on [ GitHub] ( https://github.com/Azure-Samples/communication-services-java-quickstarts/tree/main/ManageTeamsIdentityMobileAndDesktop ) .
22
+
20
23
## Set up
21
24
22
25
### Create a new Java application
@@ -62,17 +65,17 @@ Use the following code to begin:
62
65
``` java
63
66
package com.communication.quickstart ;
64
67
65
- import com.azure.communication.common.* ;
66
- import com.azure.communication.identity.* ;
67
- import com.azure.communication.identity.models.* ;
68
- import com.azure.core.credential.* ;
68
+ import com.azure.communication.identity.CommunicationIdentityClient ;
69
+ import com.azure.communication.identity.CommunicationIdentityClientBuilder ;
70
+ import com.azure.communication.identity.models.GetTokenForTeamsUserOptions ;
71
+ import com.azure.core.credential.AccessToken ;
69
72
import com.microsoft.aad.msal4j.IAuthenticationResult ;
70
73
import com.microsoft.aad.msal4j.InteractiveRequestParameters ;
71
74
import com.microsoft.aad.msal4j.PublicClientApplication ;
72
75
73
- import java.io.IOException ;
74
- import java.time.* ;
75
- import java.util.* ;
76
+ import java.net.URI ;
77
+ import java.util.HashSet ;
78
+ import java.util.Set ;
76
79
77
80
public class App
78
81
{
@@ -89,9 +92,12 @@ public class App
89
92
The first step in the token exchange flow is getting a token for your Teams user by using [ Microsoft.Identity.Client] ( ../../../active-directory/develop/reference-v2-libraries.md ) .
90
93
91
94
``` java
95
+ // You need to provide your Azure AD client ID and tenant ID
92
96
String appId = " <contoso_application_id>" ;
93
- String authority = " https://login.microsoftonline.com/<contoso_tenant_id>" ;
97
+ String tenantId = " <contoso_tenant_id>" ;
98
+ String authority = " https://login.microsoftonline.com/" + tenantId;
94
99
100
+ // Create an instance of PublicClientApplication
95
101
PublicClientApplication pca = PublicClientApplication . builder(appId)
96
102
.authority(authority)
97
103
.build();
@@ -101,15 +107,18 @@ Set<String> scope = new HashSet<String>();
101
107
scope. add(" https://auth.msft.communication.azure.com/Teams.ManageCalls" );
102
108
scope. add(" https://auth.msft.communication.azure.com/Teams.ManageChats" );
103
109
110
+ // Create an instance of InteractiveRequestParameters for acquiring the AAD token and object ID of a Teams user
104
111
InteractiveRequestParameters parameters = InteractiveRequestParameters
105
112
.builder(new URI (redirectUri))
106
113
.scopes(scope)
107
114
.build();
108
115
116
+ // Retrieve the AAD token and object ID of a Teams user
109
117
IAuthenticationResult result = pca. acquireToken(parameters). get();
110
118
String teamsUserAadToken = result. accessToken();
111
119
String [] accountIds = result. account(). homeAccountId(). split(" \\ ." );
112
120
String userObjectId = accountIds[0 ];
121
+ System . out. println(" Teams token: " + teamsUserAadToken);
113
122
```
114
123
115
124
### Step 2: Initialize the CommunicationIdentityClient
@@ -122,6 +131,7 @@ Add the following code to the `main` method:
122
131
// You can find your connection string from your resource in the Azure portal
123
132
String connectionString = " <connection_string>" ;
124
133
134
+ // Instantiate the identity client
125
135
CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder ()
126
136
.connectionString(connectionString)
127
137
.buildClient();
@@ -132,14 +142,15 @@ CommunicationIdentityClient communicationIdentityClient = new CommunicationIdent
132
142
Use the ` getTokenForTeamsUser ` method to issue an access token for the Teams user that can be used with the Azure Communication Services SDKs.
133
143
134
144
``` java
145
+ // Exchange the Azure AD access token of the Teams User for a Communication Identity access token
135
146
GetTokenForTeamsUserOptions options = new GetTokenForTeamsUserOptions (teamsUserAadToken, appId, userObjectId);
136
147
var accessToken = communicationIdentityClient. getTokenForTeamsUser(options);
137
148
System . out. println(" Token: " + accessToken. getToken());
138
149
```
139
150
140
151
## Run the code
141
152
142
- Navigate to the directory containing the ` pom.xml ` file and compile the project by using the following ` mvn ` command.
153
+ Navigate to the directory containing the ` pom.xml ` file and compile the project by using the ` mvn compile ` command.
143
154
144
155
Then, build the package.
145
156
0 commit comments