public async Task <string> GetRoomSuggestedTime(Intranet.Models.Common.RoomManagementModel model) { string endpoint = "https://graph.microsoft.com/v1.0/me/findMeetingTimes"; string result = string.Empty; using (var client = new HttpClient()) { using (var request = new HttpRequestMessage(HttpMethod.Post, endpoint)) { request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", model.AccessToken); request.Headers.Add("Prefer", "outlook.timezone=\"SA Pacific Standard Time\""); request.Content = new StringContent("{ \"attendees\": [{ \"type\": \"resource\", \"emailAddress\": { \"name\": \"Sala Prueba\", \"address\": \"[email protected]\" } }],\"locationConstraint\": { \"isRequired\": \"true\", \"suggestLocation\": \"false\",\"locations\": [ { \"locationEmailAddress\": \"[email protected]\" }]}, \"timeConstraint\": {\"activityDomain\":\"unrestricted\", \"timeslots\": [{ \"start\": { \"dateTime\": \"" + model.StartDateTime + "\", \"timeZone\": \"SA Pacific Standard Time\" }, \"end\": { \"dateTime\": \"" + model.EndDateTime + "\", \"timeZone\": \"SA Pacific Standard Time\" } } ] }, \"meetingDuration\": \"" + model.Duration + "\", \"returnSuggestionReasons\": \"true\"}", Encoding.UTF8, "application/json"); //request.Content = new StringContent("{ \"attendees\": [{ \"type\": \"resource\", \"emailAddress\": { \"name\": \"Sala Prueba\", \"address\": \"[email protected]\" } }, { \"type\": \"resource\", \"emailAddress\": { \"name\": \"Piso 6 - Recepcion\", \"address\": \"[email protected]\" } }, { \"type\": \"resource\", \"emailAddress\": { \"name\": \"Piso 6 - Sala Reuniones\", \"address\": \"[email protected]\" } } ],\"locationConstraint\": { \"isRequired\": \"true\", \"suggestLocation\": \"false\",\"locations\": [{ \"locationEmailAddress\": \"[email protected]\" }, { \"locationEmailAddress\": \"[email protected]\" },{ \"locationEmailAddress\": \"[email protected]\" }]}, \"timeConstraint\": {\"activityDomain\":\"unrestricted\", \"timeslots\": [{ \"start\": { \"dateTime\": \"" + model.StartDateTime + "\", \"timeZone\": \"SA Pacific Standard Time\" }, \"end\": { \"dateTime\": \"" + model.EndDateTime + "\", \"timeZone\": \"SA Pacific Standard Time\" } } ] }, \"meetingDuration\": \"" + model.Duration + "\", \"returnSuggestionReasons\": \"true\"}", Encoding.UTF8, "application/json"); using (HttpResponseMessage response = await client.SendAsync(request)) { if (response.StatusCode == HttpStatusCode.OK) { var json = JObject.Parse(await response.Content.ReadAsStringAsync()); var suggestions = json.GetValue("meetingTimeSuggestions"); var roomItem = suggestions.First; if (roomItem != null) { result = roomItem.Last.First.First["locationEmailAddress"].ToString(); } } return(JsonConvert.SerializeObject(result)); } } } }
public async Task <ActionResult> About(DateTime fecha, string horasFI, string minutosFI, string ampm, string horasD, string minutosD, string todoDia) { var objResult = new EventsViewModel(); string clientID = ConfigurationManager.AppSettings["ida:ClientID"]; string aadInstance = ConfigurationManager.AppSettings["ida:AADInstance"]; string tenant = ConfigurationManager.AppSettings["ida:Tenant"]; string clientSecret = ConfigurationManager.AppSettings["ida:AppKey"]; string graphResourceID = ConfigurationManager.AppSettings["ida:GraphResourceID"]; string authority = string.Format(CultureInfo.InvariantCulture, aadInstance, tenant); var signInUserId = ClaimsPrincipal.Current.FindFirst(ClaimTypes.NameIdentifier).Value; var userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; if (Request.QueryString["SPLanguage"] != null) { objResult.CurrentCulture = new CultureInfo(Request.QueryString["SPLanguage"]); } else { objResult.CurrentCulture = System.Threading.Thread.CurrentThread.CurrentCulture; } AuthenticationContext authContext = new AuthenticationContext(authority, new ADALTokenCache(signInUserId)); try { var authResult = await authContext.AcquireTokenSilentAsync(graphResourceID, new ClientCredential(clientID, clientSecret), new UserIdentifier(userObjectId, UserIdentifierType.UniqueId)); GraphService graphService = new GraphService(); var objRoom = new Intranet.Models.Common.RoomManagementModel(); objRoom.AccessToken = authResult.AccessToken; if (ampm == "pm") { horasFI = (12 + int.Parse(horasFI)).ToString(); } objRoom.StartDateTime = string.Format("{0:yyyy-MM-dd}T{1}:{2}:00", fecha, horasFI, minutosFI); int horaFin = int.Parse(horasFI) + int.Parse(horasD); int minutosFin = int.Parse(minutosFI) + int.Parse(minutosD); if (minutosFin == 60) { minutosFin = 0; horaFin++; } objRoom.EndDateTime = string.Format("{0:yyyy-MM-dd}T{1:00}:{2:00}:00", fecha, horaFin, minutosFin); objRoom.Duration = "PT"; if (horasD != "0") { objRoom.Duration = string.Format("{0}{1}H", objRoom.Duration, horasD); } if (minutosD != "00") { objRoom.Duration = string.Format("{0}{1}M", objRoom.Duration, minutosD); } var tmpResult = await graphService.GetRoomSuggestedTime(objRoom); var roomAddress = Newtonsoft.Json.JsonConvert.DeserializeObject <string>(tmpResult); if (!string.IsNullOrEmpty(roomAddress)) { var objMeeting = new Intranet.Models.Common.CreateEventModel(); objMeeting.AccessToken = objRoom.AccessToken; objMeeting.EndDateTime = objRoom.EndDateTime; objMeeting.RoomEmail = roomAddress; objMeeting.StartDateTime = objRoom.StartDateTime; tmpResult = await graphService.CreateEvent(objMeeting); ViewBag.MeetingUrl = Newtonsoft.Json.JsonConvert.DeserializeObject <string>(tmpResult).Replace("&", "&"); } } catch (AdalException exception) { objResult.Message = "No se puede obtener sus datos de sesión. Cierre la ventana y vuelva a ingresar"; //handle token acquisition failure if (exception.ErrorCode == AdalError.FailedToAcquireTokenSilently) { authContext.TokenCache.Clear(); //handle token acquisition failure } } return(View()); }