Skip to content

Commit 04e0d91

Browse files
Merge pull request #210358 from AikoBB/aigerimb/acs-identity/cte-ga-link-to-quickstart
Final code section for CTE Java&Python
2 parents 01b9030 + 0810edd commit 04e0d91

File tree

2 files changed

+34
-16
lines changed

2 files changed

+34
-16
lines changed

articles/communication-services/quickstarts/includes/manage-teams-identity-java.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
6366
package 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;
6972
import com.microsoft.aad.msal4j.IAuthenticationResult;
7073
import com.microsoft.aad.msal4j.InteractiveRequestParameters;
7174
import 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

7780
public class App
7881
{
@@ -89,9 +92,12 @@ public class App
8992
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).
9093

9194
```java
95+
// You need to provide your Azure AD client ID and tenant ID
9296
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;
9499

100+
// Create an instance of PublicClientApplication
95101
PublicClientApplication pca = PublicClientApplication.builder(appId)
96102
.authority(authority)
97103
.build();
@@ -101,15 +107,18 @@ Set<String> scope = new HashSet<String>();
101107
scope.add("https://auth.msft.communication.azure.com/Teams.ManageCalls");
102108
scope.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
104111
InteractiveRequestParameters 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
109117
IAuthenticationResult result = pca.acquireToken(parameters).get();
110118
String teamsUserAadToken = result.accessToken();
111119
String[] accountIds = result.account().homeAccountId().split("\\.");
112120
String 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
123132
String connectionString = "<connection_string>";
124133

134+
// Instantiate the identity client
125135
CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder()
126136
.connectionString(connectionString)
127137
.buildClient();
@@ -132,14 +142,15 @@ CommunicationIdentityClient communicationIdentityClient = new CommunicationIdent
132142
Use 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
135146
GetTokenForTeamsUserOptions options = new GetTokenForTeamsUserOptions(teamsUserAadToken, appId, userObjectId);
136147
var accessToken = communicationIdentityClient.getTokenForTeamsUser(options);
137148
System.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

144155
Then, build the package.
145156

articles/communication-services/quickstarts/includes/manage-teams-identity-python.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ ms.author: gistefan
1515

1616
## Set up prerequisites
1717

18-
- [Python](https://www.python.org/downloads/) 2.7 or 3.6+.
18+
- [Python](https://www.python.org/downloads/) 3.7+.
19+
20+
## Final code
21+
Find the finalized code for this quickstart on [GitHub](https://github.com/Azure-Samples/communication-services-python-quickstarts/tree/main/manage-teams-identity-mobile-and-desktop).
1922

2023
## Set up
2124

@@ -32,13 +35,13 @@ ms.author: gistefan
3235
```python
3336
import os
3437
from azure.communication.identity import CommunicationIdentityClient, CommunicationUserIdentifier
38+
from msal.application import PublicClientApplication
3539

3640
try:
3741
print("Azure Communication Services - Access Tokens Quickstart")
3842
# Quickstart code goes here
3943
except Exception as ex:
40-
print("Exception:")
41-
print(ex)
44+
print(f"Exception: {ex}")
4245
```
4346

4447
### Install the package
@@ -55,18 +58,21 @@ pip install msal
5558
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). In Azure portal, configure the Redirect URI of your "Mobile and Desktop application" as `http://localhost`.
5659

5760
```python
58-
from msal.application import PublicClientApplication
59-
60-
client_id = "<contoso_application_id>"
61-
tenant_id = "<contoso_tenant_id>"
61+
# This code demonstrates how to fetch your Azure AD client ID and tenant ID
62+
# from an environment variable.
63+
client_id = os.environ["AAD_CLIENT_ID"]
64+
tenant_id = os.environ["AAD_TENANT_ID"]
6265
authority = "https://login.microsoftonline.com/%s" % tenant_id
6366

67+
# Create an instance of PublicClientApplication
6468
app = PublicClientApplication(client_id, authority=authority)
6569

6670
scopes = [
6771
"https://auth.msft.communication.azure.com/Teams.ManageCalls",
6872
"https://auth.msft.communication.azure.com/Teams.ManageChats"
6973
]
74+
75+
# Retrieve the AAD token and object ID of a Teams user
7076
result = app.acquire_token_interactive(scopes)
7177
aad_token = result["access_token"]
7278
user_object_id = result["id_token_claims"]["oid"]
@@ -92,6 +98,7 @@ client = CommunicationIdentityClient.from_connection_string(connection_string)
9298
Use the `get_token_for_teams_user` method to issue an access token for the Teams user that can be used with the Azure Communication Services SDKs.
9399

94100
```python
101+
# Exchange the Azure AD access token of the Teams User for a Communication Identity access token
95102
token_result = client.get_token_for_teams_user(aad_token, client_id, user_object_id)
96103
print("Token: " + token_result.token)
97104
```

0 commit comments

Comments
 (0)