C# code to get calendar info of user from O365 -Error The remote server returned an error: (401) Unauthorized. RRS feed

  • Question

  • Hi Guys,

    I am using below code to retrieve calendar events of 5 users; but end up with error as below

    // Exchange connection ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010); // replace with proper username and password service.Credentials = new WebCredentials("", ""); service.Url = new Uri(""); // Office 365 Exchange API URL (replace it with a local server URL if you are using a local Exchange installation) // this week DateTime startDate = DayPilot.Utils.Week.FirstDayOfWeek(); DateTime endDate = startDate.AddDays(7); //ExchangeService Service = null; // load the default calendar CalendarFolder calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet()); // load events CalendarView cView = new CalendarView(startDate, endDate, 50); cView.PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End, AppointmentSchema.Id); FindItemsResults<Appointment> appointments = calendar.FindAppointments(cView);

      Message=The request failed. The remote server returned an error: (401) Unauthorized.
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
       at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
       at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder(FolderId folderId, PropertySet propertySet)
       at Microsoft.Exchange.WebServices.Data.ExchangeService.BindToFolder[TFolder](FolderId folderId, PropertySet propertySet)
       at Microsoft.Exchange.WebServices.Data.CalendarFolder.Bind(ExchangeService service, WellKnownFolderName name, PropertySet propertySet)
       at ConsoleApp3.Program.Main(String[] args) in C:\Users\xyz\Documents\Visual Studio 2017\Projects\ConsoleApp3\ConsoleApp3\Program.cs:line 34

    Inner Exception 1:
    WebException: The remote server returned an error: (401) Unauthorized.


    Friday, June 8, 2018 5:24 PM

All replies

  • The credentials presented by the client program does not have authority to access the resource is what the 401 error means. 

    You can post to the office forum for help.

    • Proposed as answer by Dolen Zhang Wednesday, June 20, 2018 9:04 AM
    Friday, June 8, 2018 8:28 PM