EWS - The remote server returned an error: (401) Unauthorized
-
Tuesday, January 22, 2013 7:18 PM
I’m developing client application, which connects to a mailbox then reads/parses messages and updates client database. The company uses Office 365. I’m using Managed APIs :
var _service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);
_service.TraceEnabled = true;
_service.Credentials = new System.Net.NetworkCredential("user_id", "user_pwd");
AutodiscoverService ads = new AutodiscoverService();
ads.EnableScpLookup = false;
ads.RedirectionUrlValidationCallback = delegate { return true; };
GetUserSettingsResponse grResp = ads.GetUserSettings("user_id@domain_name.com", UserSettingName.InternalEwsUrl);
Uri casURI = new Uri(grResp.Settings[UserSettingName.ExternalEwsUrl].ToString());
_service.Url = casURI;
Line starting “ads.GetUserSettings..” gives the error “The remote server returned an error: (401) Unauthorized…” even though I logged as the Exchange Admin...
All Replies
-
Tuesday, January 22, 2013 11:15 PM
You need to set the credentials property on your AutodiscoverService object (this is a seperate object from the ExchangeService object) eg
AutodiscoverService.Credentials = new System.Net.NetworkCredential("user_id","user_pwd");
You also have another bug in your code your requesting the InternalEWSURL then trying to access the ExternalEwsUrl eg
GetUserSettingsResponse grResp = ads.GetUserSettings("user_id@domain_name.com", UserSettingName.InternalEwsUrl); Uri casURI = new Uri(grResp.Settings[UserSettingName.ExternalEwsUrl].ToString()); Should be GetUserSettingsResponse grResp = ads.GetUserSettings("user_id@domain_name.com", UserSettingName.ExternalEwsUrl); Uri casURI = new Uri(grResp.Settings[UserSettingName.ExternalEwsUrl].ToString());Cheers
Glen

