我们以编程方式(使用 Microsoft Exchange Web Services Managed API 2.0)访问用户的日历(在 Exchange 2010 SP1 上)。
我们已经能够通过我们的开发环境中的自动发现成功地与 EWS 通信,我们不必在 Exchange 上进行任何特定设置(我们使用默认的开箱即用设置)。
不幸的是,这在客户端的环境中不起作用。客户端没有测试环境。我们应该与他们的实时 Exchange 服务器进行通信。
最初,自动发现在客户端环境中不起作用。我们收到以下错误:
Microsoft.Exchange.WebServices.Data.AutodiscoverLocalException: The Autodiscover service couldn't be located.
at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings[TSettings](String emailAddress, List`1 redirectionEmailAddresses, Int32& currentHop)
at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetLegacyUserSettings[TSettings](String emailAddress)
at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.InternalGetLegacyUserSettings(String emailAddress, List`1 requestedSettings)
at Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverService.GetUserSettings(String userSmtpAddress, UserSettingName[] userSettingNames)
at Microsoft.Exchange.WebServices.Data.ExchangeService.GetAutodiscoverUrl(String emailAddress, ExchangeVersion requestedServerVersion, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback)
at Microsoft.Exchange.WebServices.Data.ExchangeService.AutodiscoverUrl(String emailAddress, AutodiscoverRedirectionUrlValidationCallback validateRedirectionUrlCallback)
所以现在我们明确指定 EWS 的 URL。这给了我们以下错误:
Microsoft.Exchange.WebServices.Data.ServiceRequestException: The request failed. The remote server returned an error: (401) Unauthorized. ---> System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(HttpWebRequest request)
--- End of inner exception stack trace ---
at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(HttpWebRequest request)
at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
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, FolderId id)
异常在以下代码的第 5 行引发:
ServicePointManager.ServerCertificateValidationCallback = this.CertificateValidationCallBack;
ExchangeService exchangeWebService = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
exchangeWebService.Credentials = new WebCredentials("username@domain.local", "myPassword");
exchangeWebService.AutodiscoverUrl("username@domain.local", this.RedirectionUrlValidationCallback);
**CalendarFolder calendarFolder = CalendarFolder.Bind(exchangeWebService, new FolderId(WellKnownFolderName.Calendar, userName));**
CalendarView calendarView = new CalendarView(startDate, endDate);
calendarView.PropertySet = new PropertySet(BasePropertySet.IdOnly, AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.IsRecurring, AppointmentSchema.AppointmentType, AppointmentSchema.End, AppointmentSchema.Duration);
FindItemsResults<Appointment> findResults = calendarFolder.FindAppointments(calendarView);
我们不介意自动发现不起作用,因为我们可以明确指定 EWS 的 URL。我们想知道的是什么设置、权限等,我们需要检查客户端的 Exchange 实例以确定引发上述异常 (ServiceRequestException) 的原因。
我们已请求在客户端 Exchange 实例上的 Exchange 命令行管理程序上执行以下命令:
Test-OutlookWebServices –Identity username
Get-OrganizationConfig
我们还没有收到结果。请让我们知道是否还有其他需要检查的地方。