I’m developing a small Windows form app to test Graph API functions. I have two functionalities in the application, user's log in and get channels for specified team. I created a class that contains functions for user login and for returning channels for specified team. I have a ListView on Form in which I want to show all the channels, but when I call a function for returning channels in button event, nothing happens, nothing is displayed in the ListView. Here is the code:
- public static class GraphHelper
- {
-
- public static GraphServiceClient graphClient;
- public static string token;
-
- private static string[] scopes = new string[] { "user.read" };
- public static string TokenForUser = null;
- public static DateTimeOffset expiration;
-
- private const string ClientId = "599ed98d-4356-4a96-ad37-04391e9c48dc";
-
- private const string Tenant = "common";
- private const string Authority = "https://login.microsoftonline.com/" + Tenant;
-
-
- private static IPublicClientApplication PublicClientApp;
-
- private static string MSGraphURL = "https://graph.microsoft.com/beta/";
- private static AuthenticationResult authResult;
-
- public static GraphServiceClient GetGraphClient(string token)
- {
- if (graphClient == null)
- {
-
- try
- {
- graphClient = new GraphServiceClient(
- "https://graph.microsoft.com/v1.0",
- new DelegateAuthenticationProvider(
- async (requestMessage) =>
- {
- requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", token);
-
- requestMessage.Headers.Add("SampleID", "uwp-csharp-snippets-sample");
-
- }));
- return graphClient;
- }
-
- catch (Exception ex)
- {
- Debug.WriteLine("Could not create a graph client: " + ex.Message);
- }
- }
- return graphClient;
- }
-
- public static async Task<string> GetTokenForUserAsync()
- {
- if (TokenForUser == null || expiration <= DateTimeOffset.UtcNow.AddMinutes(10))
- {
- PublicClientApp = PublicClientApplicationBuilder.Create(ClientId)
- .WithAuthority(Authority)
- .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
- .WithLogging((level, message, containsPii) =>
- {
- Debug.WriteLine($"MSAL: {level} {message} ");
- }, LogLevel.Warning, enablePiiLogging: false, enableDefaultPlatformLogging: true)
- .Build();
-
-
- IEnumerable<IAccount> accounts = await PublicClientApp.GetAccountsAsync().ConfigureAwait(false);
- IAccount firstAccount = accounts.FirstOrDefault();
-
- try
- {
- authResult = await PublicClientApp.AcquireTokenSilent(scopes, firstAccount)
- .ExecuteAsync();
- }
- catch (MsalUiRequiredException ex)
- {
-
- Debug.WriteLine($"MsalUiRequiredException: {ex.Message}");
-
- authResult = await PublicClientApp.AcquireTokenInteractive(scopes)
- .ExecuteAsync()
- .ConfigureAwait(false);
- }
-
- TokenForUser = authResult.AccessToken;
- }
-
- return TokenForUser;
- }
-
- public static async Task<User> GetMeAsync(string token)
- {
- GraphHelper.graphClient = GraphHelper.GetGraphClient(token);
- try
- {
-
- return await GraphHelper.graphClient.Me
- .Request()
- .Select(u => new
- {
- u.DisplayName
- })
- .GetAsync();
- }
- catch (ServiceException ex)
- {
- Console.WriteLine($"Error getting signed-in user: {ex.Message}");
- return null;
- }
- }
- public static async Task<IEnumerable<Channel>> GetChannels(string teamId)
- {
- graphClient = GetGraphClient(token);
-
- var channels = await graphClient.Teams[teamId].Channels
- .Request()
- .GetAsync();
-
- return channels;
- }
- }
-
- public partial class Form1 : Form
- {
- public static GraphServiceClient graphClient;
- public static string token;
- public Form1()
- {
- InitializeComponent();
- }
-
- private async void button1_Click(object sender, EventArgs e)
- {
- token = await GraphHelper.GetTokenForUserAsync();
- User graphUser = await GraphHelper.GetMeAsync(token);
- label2.Text = graphUser.DisplayName;
- }
-
- private async void button3_Click(object sender, EventArgs e)
- {
- var channels = GraphHelper.GetChannels("8557483b-a233-4710-82de-e1bdb03bb9a9").Result;
-
- foreach (var ch in channels)
- {
- ListViewItem item = new ListViewItem(new string[] { ch.DisplayName, ch.Id});
- listView1.Items.Add(item);
- }
- }
- }
Does anyone know how to solve this?