Get Microsoft Teams Channel’s Email via API

In this blog post, I will explain you how to retrieve the email address associated with a Channel in Microsoft Teams via API.

I will assume that you know how to retrieve a bearer token from Azure Active Directory App and how to retrieve a Channel ID in MS Teams.

Please bear in mind this is undocumented from Microsoft and this code might need changes in the future (especially for the endpoint in beta).

What do you need to know?

This code will use delegate permissions in order to retrieve the information. This means you will need the user to be at least a member of the Teams in which you would like to retrieve the information.

What to replace ?

[MYGUIDTENANT] : replace with the Guid of your tenant

[USEREMAIL] : the user email of the user you would like to use the impersonation

[USERPWD] : the user password of the user you would like to use the impersonation

[CHANNELID] : replace with the Channel ID you would like to retrieve the email address. Example of ID :   19:d4b177fg1e6221c784641523d7h23549

            string nativeapp = "1fec8e78-bce4-4aaf-ab1b-5451cc387264";
            string tenantId = "[MYGUIDTENANT]";
            UserPasswordCredential uscr = new UserPasswordCredential("[USEREMAIL]", "[USERPWD]");
            AuthenticationContext authenticationContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}/", tenantId), false);

            string accessToken = authenticationContext.AcquireTokenAsync("https://api.spaces.skype.com", nativeapp, uscr).Result.AccessToken;
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken);
            var response = client.GetAsync("https://teams.microsoft.com/api/mt/emea/beta/channels/[CHANNELID]@thread.skype/email").Result;

            Console.WriteLine(response.Content.ReadAsStringAsync().Result);

This entry was posted in Office 365, Teams and tagged , , , , . Bookmark the permalink.

Leave a comment