title: EWS Oauth Exception: The request failed. The remote server returned an error: (401) Unauthorized
body: <p>I'm trying to create a bot that can book meetings. In order to do that i need to access the calendar of an employee to get FreeBusy info to ultimately book a meeting. I'm trying to avoid hardcoding the email and password and for that I want to use an access token from Azure AD to call EWS.
  I set the properties for </p>
  
  <pre><code>public static ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
  </code></pre>
  
  <p>using this method:</p>
  
  <pre><code>       public static async System.Threading.Tasks.Task UseExchangeService(IDialogContext context, string userEmailAddress, SecureString     userPassword)
           {
          string authority = ConfigurationManager.AppSettings["authority"];
          string clientID = ConfigurationManager.AppSettings["clientID"];
           string resource = ConfigurationManager.AppSettings["resource"];
          string appKey = ConfigurationManager.AppSettings["appkey"];
  
          AuthenticationContext authenticationContext = new AuthenticationContext(authority, false);
          ClientCredential clientCred = new ClientCredential(clientID, appKey);
          AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resource, clientCred);
          service.Url = new Uri(ConfigurationManager.AppSettings["serverName"] + "/ews/exchange.asmx");
          service.TraceEnabled = true;
          service.TraceFlags = TraceFlags.All;
          service.Credentials = new OAuthCredentials(authenticationResult.AccessToken);
  
  
          // USING THIS LINE IT WORKS FINE!
          // service.Credentials = new NetworkCredential(userEmailAddress, userPassword); // VIRKER
  
      }
  </code></pre>
  
  <p>I do get the access token from Azure AD and I have granted the permission for the application in Azure AD.</p>
  
  <p>I use this method to extract the freebusytimes, it contains other more code to display the times as buttons on a herocard, but this is the call to EWS:</p>
  
  <pre><code>       List&lt;AttendeeInfo&gt; attendees = new List&lt;AttendeeInfo&gt;();
          attendees.Add(new AttendeeInfo()
          {
              SmtpAddress = "MyEMAIL",
              AttendeeType = MeetingAttendeeType.Organizer
          });
  
          attendees.Add(new AttendeeInfo()
          {
              SmtpAddress = BookersEmail,
              AttendeeType = MeetingAttendeeType.Required
          });
          //DateTime date1 = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day.Ad, 7, 0, 0)
          // Specify options to request free/busy information and suggested meeting times.
          AvailabilityOptions availabilityOptions = new AvailabilityOptions();
          availabilityOptions.GoodSuggestionThreshold = 49;
          availabilityOptions.MaximumNonWorkHoursSuggestionsPerDay = 0;
          availabilityOptions.MaximumSuggestionsPerDay = 20;
          // Note that 60 minutes is the default value for MeetingDuration, but setting it explicitly for demonstration purposes.
          availabilityOptions.MeetingDuration = 60;
          availabilityOptions.MinimumSuggestionQuality = SuggestionQuality.Excellent;
  
          //TimeWindow hej = new TimeWindow();
          DateTime StartDay = DateTime.Now.AddDays(1);
          TimeSpan ts = new TimeSpan(9, 0, 0);
          DateTime StartTime = StartDay.Date + ts;
  
          availabilityOptions.DetailedSuggestionsWindow = new TimeWindow(StartTime, DateTime.Now.AddDays(4));
          availabilityOptions.RequestedFreeBusyView = FreeBusyViewType.FreeBusy;
          // Return free/busy information and a set of suggested meeting times. 
          // This method results in a GetUserAvailabilityRequest call to EWS.
          GetUserAvailabilityResults results = service.GetUserAvailability(attendees,
                                                                           availabilityOptions.DetailedSuggestionsWindow,
                                                                           AvailabilityData.FreeBusyAndSuggestions,
                                                                           availabilityOptions);
  </code></pre>
  
  <p>I have created the application in Azure AD and I have granted the following permissions:
  Office 365 Exchange Online:</p>
  
  <p>Use Exchange Web Services with full access to all mailboxes</p>
  
  <p>Read and write calendars in all mailboxes</p>
  
  <p>Read and write user and shared calendars</p>
  
  <p>Access mailboxes as the signed-in user via Exchange Web Services</p>
  
  <p>I've tried other answers i found on stackoverflow, however they do not do the trick for me. 
  Hope You can help</p>