@@ -17,6 +17,9 @@ ms.author: gistefan
1717- [ Java Development Kit (JDK)] ( /azure/developer/java/fundamentals/java-jdk-install ) version 8 or above.
1818- [ Apache Maven] ( https://maven.apache.org/download.cgi ) .
1919
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+
2023## Set up
2124
2225### Create a new Java application
@@ -62,17 +65,17 @@ Use the following code to begin:
6265``` java
6366package com.communication.quickstart ;
6467
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 ;
6972import com.microsoft.aad.msal4j.IAuthenticationResult ;
7073import com.microsoft.aad.msal4j.InteractiveRequestParameters ;
7174import com.microsoft.aad.msal4j.PublicClientApplication ;
7275
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 ;
7679
7780public class App
7881{
@@ -89,9 +92,12 @@ public class App
8992The 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 ) .
9093
9194``` java
95+ // You need to provide your Azure AD client ID and tenant ID
9296String 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;
9499
100+ // Create an instance of PublicClientApplication
95101PublicClientApplication pca = PublicClientApplication . builder(appId)
96102 .authority(authority)
97103 .build();
@@ -101,15 +107,18 @@ Set<String> scope = new HashSet<String>();
101107scope. add(" https://auth.msft.communication.azure.com/Teams.ManageCalls" );
102108scope. add(" https://auth.msft.communication.azure.com/Teams.ManageChats" );
103109
110+ // Create an instance of InteractiveRequestParameters for acquiring the AAD token and object ID of a Teams user
104111InteractiveRequestParameters parameters = InteractiveRequestParameters
105112 .builder(new URI (redirectUri))
106113 .scopes(scope)
107114 .build();
108115
116+ // Retrieve the AAD token and object ID of a Teams user
109117IAuthenticationResult result = pca. acquireToken(parameters). get();
110118String teamsUserAadToken = result. accessToken();
111119String [] accountIds = result. account(). homeAccountId(). split(" \\ ." );
112120String userObjectId = accountIds[0 ];
121+ System . out. println(" Teams token: " + teamsUserAadToken);
113122```
114123
115124### Step 2: Initialize the CommunicationIdentityClient
@@ -122,6 +131,7 @@ Add the following code to the `main` method:
122131// You can find your connection string from your resource in the Azure portal
123132String connectionString = " <connection_string>" ;
124133
134+ // Instantiate the identity client
125135CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder ()
126136 .connectionString(connectionString)
127137 .buildClient();
@@ -132,14 +142,15 @@ CommunicationIdentityClient communicationIdentityClient = new CommunicationIdent
132142Use the ` getTokenForTeamsUser ` method to issue an access token for the Teams user that can be used with the Azure Communication Services SDKs.
133143
134144``` java
145+ // Exchange the Azure AD access token of the Teams User for a Communication Identity access token
135146GetTokenForTeamsUserOptions options = new GetTokenForTeamsUserOptions (teamsUserAadToken, appId, userObjectId);
136147var accessToken = communicationIdentityClient. getTokenForTeamsUser(options);
137148System . out. println(" Token: " + accessToken. getToken());
138149```
139150
140151## Run the code
141152
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.
143154
144155Then, build the package.
145156
0 commit comments