Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
In this exercise you'll learn how to dynamically retrieve user identity and token values from Azure Communication Services using Azure Functions. Once retrieved, the values will be passed to the ACS UI composite to enable a call to be made by a customer.
Open local.settings.json and update the
ACS_CONNECTION_STRINGvalue with the ACS connection string you saved in an earlier exercise.Open Startup.cs in Visual Studio and explore the second
AddSingleton()call in theConfigureServices()method.var host = new HostBuilder() .ConfigureFunctionsWebApplication() .ConfigureServices(services => { ... services.AddSingleton(static p => { var config = p.GetRequiredService<IConfiguration>(); var connectionString = config.GetValue<string>("ACS_CONNECTION_STRING"); return new CommunicationIdentityClient(connectionString); }); ... }) .Build(); }The
AddSingleton()call creates aCommunicationIdentityClientobject using theACS_CONNECTION_STRINGvalue from local.settings.json.Open ACSTokenFunction.cs and locate the constructor and field definitions.
A field named
Scopesis defined that includes theCommunicationTokenScope.VoIPscope. This scope is used to create the access token for the video call.private static readonly CommunicationTokenScope[] Scopes = [ CommunicationTokenScope.VoIP, ];The
CommunicationIdentityClientsingleton instance created in Startup.cs is injected into the constructor and assigned to the_tokenClientfield.private readonly CommunicationIdentityClient _tokenClient; public ACSTokenFunction(CommunicationIdentityClient tokenClient) { _tokenClient = tokenClient; }
Explore the
Run()method in ACSTokenFunction.cs:[Function("HttpTriggerAcsToken")] public async Task<HttpResponseData> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequestData req, ILogger log) { var user = await _tokenClient.CreateUserAsync(); var userToken = await _tokenClient.GetTokenAsync(user, Scopes); var response = req.CreateResponse(HttpStatusCode.OK); await response.WriteAsJsonAsync( new { userId = user.Value.Id, userToken.Value.Token, userToken.Value.ExpiresOn } ); return response; }- It defines a function named of
HttpTriggerAcsTokenthat can be called with an HTTP GET request. - A new ACS user is created by calling the
_tokenClient.CreateUserAsync()method. - An access token used for video calls is created by calling the
_tokenClient. GetTokenAsync()method. - The user ID and token are returned from the function as a JSON object.
- It defines a function named of
Run the program by pressing F5 in Visual Studio or by selecting Debug --> Start Debugging from the menu. This will start the Azure Functions project and make the
ACSTokenFunctionavailable to call.Note
If you're using VS Code you can open a terminal window in the GraphACSFunctions folder and run
func start. This assumes that you have the Azure Functions Core Tools installed on your machine.Now that the Azure Functions are running locally, the client needs to be able to call into them to get the ACS user identity and token values.
Open samples/acs-to-teams-meeting/client/react/App.tsx file in your editor.
Locate the
userIdandtokenstate variables in the component. Remove the hardcoded values and replace them with empty quotes:const [userId, setUserId] = useState<string>(''); const [token, setToken] = useState<string>('');Locate the
useEffectfunction and change it to look like the following to enable calling the Azure Function to retrieve an ACS user identity and token:useEffect(() => { const init = async () => { setMessage('Getting ACS user'); //Call Azure Function to get the ACS user identity and token let res = await fetch(process.env.REACT_APP_ACS_USER_FUNCTION as string); let user = await res.json(); setUserId(user.userId); setToken(user.token); setMessage('Getting Teams meeting link...'); //Call Azure Function to get the meeting link res = await fetch(process.env.REACT_APP_TEAMS_MEETING_FUNCTION as string); let link = await res.text(); setTeamsMeetingLink(link); setMessage(''); console.log('Teams meeting link', link); } init(); }, []);Save the file before continuing.
Open a terminal window and run
npm startin thereactfolder. After it builds and loads you should see the ACS calling UI displayed and you can call into the Teams meeting that was dynamically created by Microsoft Graph.Stop the React app by pressing Ctrl + C in the terminal window.
Stop the Azure Functions project.
Commit your git changes and push them to your GitHub repository using Visual Studio Code:
- Select the Source Control icon (3rd one down in the Visual Studio Code toolbar).
- Enter a commit message and select Commit.
- Select Sync Changes.