private void selectDeviceFromGraph(object sender, SelectionChangedEventArgs e) { //device is selected from the graph device = ((sender as ListBox).SelectedItem as DeviceFromGraphModel); this.LaunchURIButton.Visibility = Visibility.Visible; this.AppServiceButton.Visibility = Visibility.Visible; //GOAL:send a command }
private async void CallGraphButton_Click(object sender, RoutedEventArgs e) { AuthenticationResult authResult = null; ResultText.Text = string.Empty; //TokenInfoText.Text = string.Empty; IEnumerable <IAccount> accounts = await App.PublicClientApp.GetAccountsAsync(); IAccount firstAccount = accounts.FirstOrDefault(); try { authResult = await App.PublicClientApp.AcquireTokenSilentAsync(scopes, firstAccount); } catch (MsalUiRequiredException ex) { // A MsalUiRequiredException happened on AcquireTokenSilentAsync. This indicates you need to call AcquireTokenAsync to acquire a token System.Diagnostics.Debug.WriteLine($"MsalUiRequiredException: {ex.Message}"); try { authResult = await App.PublicClientApp.AcquireTokenAsync(scopes); } catch (MsalException msalex) { ResultText.Text = $"Error Acquiring Token:{System.Environment.NewLine}{msalex}"; } } catch (Exception ex) { ResultText.Text = $"Error Acquiring Token Silently:{System.Environment.NewLine}{ex}"; return; } if (authResult != null) { ResultText.Text = await GetHttpContentWithToken(graphAPIEndpoint, authResult.AccessToken); tokenToUse = authResult.AccessToken; Debug.WriteLine(tokenToUse); this.SignOutButton.Visibility = Visibility.Visible; //CODE FOR ROME HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenToUse); Uri uri = new Uri("https://graph.microsoft.com/beta/me/devices"); HttpResponseMessage httpResponse = await client.GetAsync(uri); var responseText = await httpResponse.Content.ReadAsStringAsync(); Debug.WriteLine(responseText); Debug.WriteLine(responseText); Newtonsoft.Json.Linq.JObject jObject = Newtonsoft.Json.Linq.JObject.Parse(responseText); dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(responseText); var devices = obj.value; foreach (var device_model in devices) { DeviceFromGraphModel device = new DeviceFromGraphModel(); device.id = device_model.id; device.Name = device_model.Name; devicesFromGraph.Add(device); } foreach (DeviceFromGraphModel m_device in devicesFromGraph) { Debug.WriteLine(m_device.id); } //show the names on the list in hte MSGraphPageXAML //SessionList.ItemsSource = sessionNames; //inXAML instead of DeviceName use Name DeviceFromGraphList.ItemsSource = devicesFromGraph; } //further options to select the device and send a command will come from @selectDeviceFromGraph where the user selects the device //selected device will then send a command saying "hello world" }