|
| 1 | +--- |
| 2 | +title: include file |
| 3 | +description: include file |
| 4 | +services: azure-communication-services |
| 5 | +author: gistefan |
| 6 | +manager: soricos |
| 7 | +ms.service: azure-communication-services |
| 8 | +ms.subservice: azure-communication-services |
| 9 | +ms.date: 10/08/2021 |
| 10 | +ms.topic: include |
| 11 | +ms.custom: include file |
| 12 | +ms.author: gistefan |
| 13 | +--- |
| 14 | + |
| 15 | +## Set up prerequisites |
| 16 | + |
| 17 | +- [Java Development Kit (JDK)](/azure/developer/java/fundamentals/java-jdk-install) version 8 or above. |
| 18 | +- [Apache Maven](https://maven.apache.org/download.cgi). |
| 19 | + |
| 20 | +## Set up |
| 21 | + |
| 22 | +### Create a new Java application |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +```console |
| 27 | +mvn archetype:generate -DgroupId=com.communication.quickstart -DartifactId=communication-quickstart -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false |
| 28 | +``` |
| 29 | + |
| 30 | +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. |
| 31 | + |
| 32 | +### Install the package |
| 33 | + |
| 34 | +Open the `pom.xml` file in your text editor. Add the following dependency elements to the group of dependencies. |
| 35 | + |
| 36 | +```xml |
| 37 | +<dependencies> |
| 38 | + <dependency> |
| 39 | + <groupId>com.azure</groupId> |
| 40 | + <artifactId>azure-communication-identity</artifactId> |
| 41 | + <version>1.2.0-beta.1</version> |
| 42 | + </dependency> |
| 43 | + <dependency> |
| 44 | + <groupId>com.microsoft.azure</groupId> |
| 45 | + <artifactId>msal4j</artifactId> |
| 46 | + <version>1.11.0</version> |
| 47 | + </dependency> |
| 48 | +</dependencies> |
| 49 | +``` |
| 50 | + |
| 51 | +### Set up the app framework |
| 52 | + |
| 53 | +From the project directory: |
| 54 | + |
| 55 | +1. Navigate to the `/src/main/java/com/communication/quickstart` directory |
| 56 | +1. Open the `App.java` file in your editor |
| 57 | +1. Replace the `System.out.println("Hello world!");` statement |
| 58 | +1. Add `import` directives |
| 59 | + |
| 60 | +Use the following code to begin: |
| 61 | + |
| 62 | +```java |
| 63 | +package com.communication.quickstart; |
| 64 | + |
| 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.*; |
| 69 | +import com.microsoft.aad.msal4j.IAuthenticationResult; |
| 70 | +import com.microsoft.aad.msal4j.InteractiveRequestParameters; |
| 71 | +import com.microsoft.aad.msal4j.PublicClientApplication; |
| 72 | + |
| 73 | +import java.io.IOException; |
| 74 | +import java.time.*; |
| 75 | +import java.util.*; |
| 76 | + |
| 77 | +public class App |
| 78 | +{ |
| 79 | + public static void main( String[] args ) throws Exception |
| 80 | + { |
| 81 | + System.out.println("Azure Communication Services - Communication access token Quickstart"); |
| 82 | + // Quickstart code goes here |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +### Step 1: Receive the Azure Active Directory user token via the MSAL library |
| 88 | + |
| 89 | +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 | + |
| 91 | +```java |
| 92 | +String appId = "<contoso_application_id>"; |
| 93 | +String authority = "https://login.microsoftonline.com/<contoso_tenant_id>"; |
| 94 | + |
| 95 | +PublicClientApplication pca = PublicClientApplication.builder(appId) |
| 96 | + .authority(authority) |
| 97 | + .build(); |
| 98 | + |
| 99 | +String redirectUri = "http://localhost"; |
| 100 | +Set<String> scope = new HashSet<String>(); |
| 101 | +scope.add("https://auth.msft.communication.azure.com/VoIP"); |
| 102 | + |
| 103 | +InteractiveRequestParameters parameters = InteractiveRequestParameters |
| 104 | + .builder(new URI(redirectUri)) |
| 105 | + .scopes(scope) |
| 106 | + .build(); |
| 107 | + |
| 108 | +IAuthenticationResult result = pca.acquireToken(parameters).get(); |
| 109 | +``` |
| 110 | + |
| 111 | +### Step 2: Initialize the CommunicationIdentityClient |
| 112 | + |
| 113 | +Instantiate a `CommunicationIdentityClient` with your resource's access key and endpoint. Learn how to [manage your resource's connection string](../create-communication-resource.md#store-your-connection-string). In addition, you can initialize the client with any custom HTTP client the implements the `com.azure.core.http.HttpClient` interface. |
| 114 | + |
| 115 | +Add the following code to the `main` method: |
| 116 | + |
| 117 | +```java |
| 118 | +//You can find your connection string from your resource in the Azure portal |
| 119 | +String connectionString = "<connection_string>"; |
| 120 | + |
| 121 | +CommunicationIdentityClient communicationIdentityClient = new CommunicationIdentityClientBuilder() |
| 122 | + .connectionString(connectionString) |
| 123 | + .buildClient(); |
| 124 | +``` |
| 125 | + |
| 126 | +### Step 3: Exchange the Azure AD user token for the Teams access token |
| 127 | + |
| 128 | +Use the `getTokenForTeamsUser` method to issue an access token for the Teams user that can be used with the Azure Communication Services SDKs. |
| 129 | + |
| 130 | +```java |
| 131 | +var accessToken = communicationIdentityClient.getTokenForTeamsUser(result.accessToken()); |
| 132 | +System.out.println("Token: " + accessToken.getToken()); |
| 133 | +``` |
| 134 | + |
| 135 | +## Run the code |
| 136 | + |
| 137 | +Navigate to the directory containing the `pom.xml` file and compile the project by using the following `mvn` command. |
| 138 | + |
| 139 | +Then, build the package. |
| 140 | + |
| 141 | +```console |
| 142 | +mvn package |
| 143 | +``` |
| 144 | + |
| 145 | +Run the following `mvn` command to execute the app. |
| 146 | + |
| 147 | +```console |
| 148 | +mvn exec:java -Dexec.mainClass="com.communication.quickstart.App" -Dexec.cleanupDaemonThreads=false |
| 149 | +``` |
0 commit comments