ews-java-api icon indicating copy to clipboard operation
ews-java-api copied to clipboard

Unable to authenticate office 365 account(microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException)-403

Open krishna-prasadh opened this issue 5 years ago • 14 comments

Hi, We are using ews 2.0 to integrate the office 365 calendar. It is working fine in the local environment(localhost) but when we push the same code to a server(ec2 instance) and try to authenticate it throws 403 error. Are there any IP restrictions or any settings that should be enabled on office 365 side. I am able to integrate some accounts with the same code but for some accounts, it throws this error. please find the stack trace below.

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (403)Forbidden at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:74) at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158) at microsoft.exchange.webservices.data.core.ExchangeService.bindToFolder(ExchangeService.java:504) at microsoft.exchange.webservices.data.core.ExchangeService.bindToFolder(ExchangeService.java:523) at microsoft.exchange.webservices.data.core.service.folder.CalendarFolder.bind(CalendarFolder.java:60) at microsoft.exchange.webservices.data.core.service.folder.CalendarFolder.bind(CalendarFolder.java:108) at com.agilecrm.exchange.service.AppointmentServelt.getAppointments(AppointmentServelt.java:99) at com.agilecrm.exchange.service.AppointmentServelt.service(AppointmentServelt.java:55) at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:833) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1650) at org.eclipse.jetty.websocket.server.WebSocketUpgradeFilter.doFilter(WebSocketUpgradeFilter.java:206) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1637) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595) at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564) at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:219) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132) at org.eclipse.jetty.server.Server.handle(Server.java:561) at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:334) at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251) at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279) at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:104) at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140) at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131) at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:243) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:679) at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:597) at java.lang.Thread.run(Thread.java:748) Caused by: microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. The remote server returned an error: (403)Forbidden at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:644) at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:62) ... 42 more Caused by: microsoft.exchange.webservices.data.core.exception.http.HttpErrorException: The remote server returned an error: (403)Forbidden at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:723) at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:639) ... 43 more

Please suggest what should I do to resolve it. Thanks

krishna-prasadh avatar Apr 30 '20 14:04 krishna-prasadh

Hi! Do you specify EWS url explicitly or use Autodiscover? Could you show piece of code that throws exception and raw EWS request/response?

pkropachev avatar May 10 '20 15:05 pkropachev

Hi @pkropachev, Thanks for replying. We are specifying the EWS URL explicitly and it would look like this "https://outlook.office365.com/ews/exchange.asmx". Here is the dependency we are using <groupId>com.microsoft.ews-java-api</groupId> <artifactId>ews-java-api</artifactId> 2.0 Here is the code snippet.

String username = "[email protected]";
String password = "xxxxx";
String serverAddress = "https://outlook.office365.com/ews/exchange.asmx";
// ExchangeVersion.Exchange2010_SP2 is ------
ExchangeService exService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials(username,password);
exService.setCredentials(credentials);
URI uri = new URI(serverAddress);
exService.setUrl(uri);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startDate = formatter.parse("2020-04-29 12:00:00");
Date endDate = formatter.parse("2020-08-05 13:00:00");
CalendarFolder cf = CalendarFolder.bind(exService,WellKnownFolderName.Calendar);
FindItemsResults<Appointment> findResults = cf.findAppointments(new CalendarView(startDate, endDate));

We are facing the issues at CalendarFolder.bind(...).

krishna-prasadh avatar May 12 '20 07:05 krishna-prasadh

Hi! It's strange. Your code looks correct (of course if your users are not gmail users 😊). I faced with such problem during attempt to bind folder for newly created user. So, need to wait for a bit time after user creation on Office 365. Is this constantly reproduced for the same users? Have these users tried to log into their accounts and access to calendar (e.g. using OWA)?

pkropachev avatar May 15 '20 17:05 pkropachev

Hi, Thanks for the reply. We are able to access the calendar from the office365 account and the user was created months ago. Are there any settings that should be enabled on the user side? Yes, it's continuously producing the same error.

krishna-prasadh avatar May 18 '20 07:05 krishna-prasadh

As far as I remember there are no special settings that should changed on user side. By the way, you wrote above that it works correct in local environment. Does it work for all users in local environment?

pkropachev avatar May 21 '20 13:05 pkropachev

Yes it's working fine in the local environment. Can you share the special settings that should be enabled? we'll check them. Thanks.

krishna-prasadh avatar May 26 '20 05:05 krishna-prasadh

I just wanted to add, that I have the exact same issue, just that I am trying to read emails: Locally on my Laptop everything is fine (running the code on a websphere liberty server) When deploying this server to the cloud (in my case ibm cloudfoundry instance) I also get:

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException The request failed. The request failed. The remote server returned an error: (403)Forbidden

i activated the tracing feature and get following output:

Type:EwsResponseHttpHeaders Message:<Trace Tag="EwsResponseHttpHeaders" Tid="60" Time="2020-05-27 13:12:13Z"> 403 null X-Proxy-BackendServerStatus : 403 Server : Microsoft-IIS/10.0 X-Proxy-RoutingCorrectness : 1 X-CalculatedBETarget : AM6PR07MB4758.eurprd07.prod.outlook.com request-id : 3297d47a-2892-491f-8819-51392b5007ed X-RUM-Validated : 1 X-FEProxyInfo : AM6P194CA0071.EURP194.PROD.OUTLOOK.COM Date : Wed, 27 May 2020 13:12:12 GMT X-CalculatedFETarget : AM6P194CU002.internal.outlook.com X-BEServer : AM6PR07MB4758 X-BackEndHttpStatus : 403 X-DiagInfo : AM6PR07MB4758 Set-Cookie : exchangecookie=8337bf575a0d4362bfbb1e5d943b10d6; expires=Thu, 27-May-2021 13:12:12 GMT; path=/; secure; HttpOnly X-BeSku : WCS5 X-FEServer : AM0PR03CA0063 Content-Length : 0 X-Powered-By : ASP.NET

seitinger avatar May 27 '20 12:05 seitinger

any solution for this ?

karges avatar Nov 12 '20 19:11 karges

I got same error in AWS server, did you found any solution? Thanks in advance

nirmaljpanchal avatar Feb 03 '21 14:02 nirmaljpanchal

I am also facing the same issue.. any solution?

shailesh-sakaria avatar Feb 24 '21 09:02 shailesh-sakaria

Getting exactly the same error..... any timeline to get a solution for this?

aliasgar110 avatar Feb 24 '21 14:02 aliasgar110

hi I stumbled upon this while trying to sync outlook emails in our application . while it works on local machine does not work when the code is deployed on azure cloud

p. s it works when the application I used with O365 sso any idea on the timeline to get this fixed

onlinepk avatar Feb 24 '21 15:02 onlinepk

Hi, is there solution to this problem now?

LoveNewLife avatar May 10 '21 09:05 LoveNewLife

I'm facing the same Issue.

Request Trace:

EwsRequest - <Trace Tag="EwsRequest" Tid="30" Time="2021-06-07 20:57:37Z">
<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><soap:Header><t:RequestServerVersion Version="Exchange2010_SP2" /></soap:Header><soap:Body><m:CreateItem MessageDisposition="SaveOnly"><m:Items><t:Message><t:MimeContent CharacterSet="utf-8">[... redacted ...]</t:MimeContent></t:Message></m:Items></m:CreateItem></soap:Body></soap:Envelope>
</Trace>

Response Trace:

EwsResponseHttpHeaders - <Trace Tag="EwsResponseHttpHeaders" Tid="30" Time="2021-06-07 20:57:38Z">
403 null
X-Proxy-BackendServerStatus : 403
Server : Microsoft-IIS/10.0
X-Proxy-RoutingCorrectness : 1
X-CalculatedBETarget : ROAP284MB0014.BRAP284.PROD.OUTLOOK.COM
request-id : e957557f-6741-4a00-8a6b-9002aa27c794
X-RUM-Validated : 1
X-FEProxyInfo : ROAP284CA0067.BRAP284.PROD.OUTLOOK.COM
Date : Mon, 07 Jun 2021 20:57:38 GMT
X-CalculatedFETarget : ROAP284CU004.internal.outlook.com
X-BEServer : ROAP284MB0014
X-BackEndHttpStatus : 403
X-DiagInfo : ROAP284MB0014
Set-Cookie : exchangecookie=[redacted]; expires=Tue, 07-Jun-2022 20:57:38 GMT; path=/; secure; HttpOnly
X-BeSku : WCS6
X-FEServer : BL0PR0102CA0016
Content-Length : 0
X-Powered-By : ASP.NET
</Trace>

There isn't a response body, only headers.

Works fine locally, but not on remote server. Has anyone found a way around this?

klimber avatar Jun 07 '21 21:06 klimber