Tuesday, September 29, 2015

It doesn't always needs to be a complicated solution... EWS “The request failed. The remote server returned an error: (401) Unauthorized"

After following this MSDN article about configuring impersonation for EWS. I was ready to go test my newly created user with impersonation rights. I already had an other account with impersonation rights which worked just fine with the following piece of code.

 Try  
      Dim svc As New ExchangeService()  
      svc.Url = New Uri("https://outlook.office365.com/EWS/Exchange.asmx")  
      svc.Credentials = New WebCredentials("svc_user@domain.com", "xxxx")   
      svc.ImpersonatedUserId = New ImpersonatedUserId(ConnectingIdType.SmtpAddress, "peter@domain.com")  
      Dim mail As New EmailMessage(svc)  
      mail.From = "peter@domain.com"  
      mail.Subject = "Test Peter svc user"  
      mail.Body = "Test Body"  
      mail.ToRecipients.Add("tom@domain.com")  
      mail.Save()  
      mail.SendAndSaveCopy()  
 Catch ex As Exception  
      MsgBox(ex.ToString)  
 End Try  

But with my newly created svc_user I kept getting the following error: "The request failed. The remote server returned an error: (401) Unauthorized". Enabling the traces on my service calls gave no extra info. After googling this error I found a lot of MSDN pages with a lot of different possible solutions, but all to no avail.
After a tip from a colleague I did what I should have done a lot sooner. Navigate to https://portal.office.com and tried logging in with the svc_user. I got nice clear message stating that my password had expired or that I should change my password on first usage. After doing this everything worked just fine and I felt a bit dumb for spending a couple of hours on this issue :-)

No comments:

Post a Comment