我正在sharepoint中开发一个Provider托管应用程序,用于调用EWS服务。我正在使用托管api。
下面是我的代码,
ClientCredential cc = new ClientCredential("********-****-****-****-********e042", "b/2i******n07QKJ********0uhVVvv*****BUs=");
AuthenticationContext authenticationContext = new AuthenticationContext("https://login.windows.net/mydomain.onmicrosoft.com/");
AuthenticationResult result = authenticationContext.AcquireToken("https://outlook.office365.com/", cc);
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
service.Credentials = new OAuthCredentials(result.AccessToken);
service.Url = new Uri("https://outlook.office365.com/EWS/Exchange.asmx");
CalendarView calView = new CalendarView(DateTime.Today, DateTime.Today.AddHours(24));
Mailbox m = new Mailbox("xxxx@xxxxxxxx.onmicrosoft.com");
FolderId CalendarFolderId = new FolderId(WellKnownFolderName.Calendar, m);
FindItemsResults<Appointment> master = service.FindAppointments(CalendarFolderId, calView);
我得到的错误是401未经授权.. 以下是堆栈跟踪,
远程服务器返回错误:(401)未经授权。
描述:执行期间发生了未处理的异常 当前的网络请求。请查看堆栈跟踪了解更多信息 有关错误的信息以及它在代码中的起源。
异常详细信息:System.Net.WebException:返回远程服务器 错误:(401)未经授权。
源错误:执行期间生成了未处理的异常 当前的网络请求。有关的来源和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
堆栈追踪:
[WebException: The remote server returned an error: (401) Unauthorized.]
System.Net.HttpWebRequest.GetResponse() +6540964
Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse() +29
Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) +34
[ServiceRequestException: The request failed. The remote server returned an error: (401) Unauthorized.]
Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request) +149
Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request) +407
Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute() +36
Microsoft.Exchange.WebServices.Data.ExchangeService.FindItems(IEnumerable`1 parentFolderIds, SearchFilter searchFilter, String queryString, ViewBase view, Grouping groupBy, ServiceErrorHandling errorHandlingMode) +263
Microsoft.Exchange.WebServices.Data.ExchangeService.FindAppointments(FolderId parentFolderId, CalendarView calendarView) +62
SharePointApp2Web.Pages.Default.Page_Load(Object sender, EventArgs e) +1004
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
System.Web.UI.Control.OnLoad(EventArgs e) +92
System.Web.UI.Control.LoadRecursive() +54
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772
对此的任何想法都会有所帮助。
提前致谢