OfficeDev / ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
MIT License
869 stars 560 forks source link

Autodiscovery succeeds but error while other operations like findItem, save #477

Open pheenomenon opened 8 years ago

pheenomenon commented 8 years ago

Hello experts,

We are using EWS to connect to an exchange. The same code works for a particular exchange server but while ran against another exchange server it passes autodiscover but fails with other operation like findAppointments()

Below is the error log with trace enabled. Any pointers on what settings at 2nd exchange is coming in the way ? For privacy reasons I have anonymized the below trace.

EWS Trace logs :<Trace Tag="AutodiscoverConfiguration" Tid="1" Time="2016-01-12 23:42:32Z">
Redirection URL found: 'https://autodiscover.CUSTOM_DOMAIN.com/autodiscover/autodiscover.xml'
</Trace>
EWS Trace logs :<Trace Tag="AutodiscoverConfiguration" Tid="1" Time="2016-01-12 23:42:32Z">
Trying to call Autodiscover for interviewscheduling@CUSTOM_DOMAIN.com on https://autodiscover.CUSTOM_DOMAIN.com/autodiscover/autodiscover.xml.
</Trace>

EWS Trace logs :<Trace Tag="AutodiscoverRequest" Tid="1" Time="2016-01-12 23:42:32Z">
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006"><Request><EMailAddress>interviewscheduling@CUSTOM_DOMAIN.com</EMailAddress><AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema></Request></Autodiscover>
</Trace>
EWS Trace logs :<Trace Tag="AutodiscoverResponse" Tid="1" Time="2016-01-12 23:42:33Z">
<?xml version="1.0" encoding="utf-8"?>
<Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/responseschema/2006">
  <Response xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a">
    <User>
      <DisplayName>Configured Interview Scheduling</DisplayName>
      <LegacyDN>/o=CUSTOM_DOMAIN/ou=Exchange Administrative Group (ANONYMIZED_ID)/cn=Recipients/cn=1c23188e50064a88a0393a8ccf3f6571-dummyID</LegacyDN>
      <AutoDiscoverSMTPAddress>Interviewscheduling@CUSTOM_DOMAIN.com</AutoDiscoverSMTPAddress>
      <DeploymentId>1ca8a03c-ea1f-44cb-b15b-f1a933981e78</DeploymentId>
    </User>
    <Account>
      <AccountType>email</AccountType>
      <Action>settings</Action>
      <MicrosoftOnline>False</MicrosoftOnline>
      <Protocol>
        <Type>EXCH</Type>
        <Server>d56982ee-b3b3-429d-87fd-ce87f36174cd@CUSTOM_DOMAIN.com</Server>
        <ServerDN>/o=CUSTOM_DOMAIN/ou=Exchange Administrative Group (ANONYMIZED_ID)/cn=Configuration/cn=Servers/cn=d56982ee-b3b3-429d-87fd-ce87f36174cd@CUSTOM_DOMAIN.com</ServerDN>
        <ServerVersion>73C08450</ServerVersion>
        <MdbDN>/o=CUSTOM_DOMAIN/ou=Exchange Administrative Group (ANONYMIZED_ID)/cn=Configuration/cn=Servers/cn=d56982ee-b3b3-429d-87fd-ce87f36174cd@CUSTOM_DOMAIN.com/cn=Microsoft Private MDB</MdbDN>
        <PublicFolderServer>dummy.CUSTOM_DOMAINowa.com</PublicFolderServer>
        <AD>USMAVDC01.CSTDOM.CUSTOM_DOMAINopg.com</AD>
        <ASUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</ASUrl>
        <EwsUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</EwsUrl>
        <EmwsUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</EmwsUrl>
        <EcpUrl>https://dummy.CUSTOM_DOMAINowa.com/ecp/</EcpUrl>
        <EcpUrl-um>?rfr=olk&amp;p=customize/voicemail.aspx&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-um>
        <EcpUrl-aggr>?rfr=olk&amp;p=personalsettings/EmailSubscriptions.slab&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-aggr>
        <EcpUrl-mt>PersonalSettings/DeliveryReport.aspx?rfr=olk&amp;exsvurl=1&amp;IsOWA=&lt;IsOWA&gt;&amp;MsgID=&lt;MsgID&gt;&amp;Mbx=&lt;Mbx&gt;&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-mt>
        <EcpUrl-ret>?rfr=olk&amp;p=organize/retentionpolicytags.slab&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-ret>
        <EcpUrl-sms>?rfr=olk&amp;p=sms/textmessaging.slab&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-sms>
        <EcpUrl-photo>PersonalSettings/EditAccount.aspx?rfr=olk&amp;chgPhoto=1&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-photo>
        <EcpUrl-tm>?rfr=olk&amp;ftr=TeamMailbox&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-tm>
        <EcpUrl-tmCreating>?rfr=olk&amp;ftr=TeamMailboxCreating&amp;SPUrl=&lt;SPUrl&gt;&amp;Title=&lt;Title&gt;&amp;SPTMAppUrl=&lt;SPTMAppUrl&gt;&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-tmCreating>
        <EcpUrl-tmEditing>?rfr=olk&amp;ftr=TeamMailboxEditing&amp;Id=&lt;Id&gt;&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-tmEditing>
        <EcpUrl-extinstall>Extension/InstalledExtensions.slab?rfr=olk&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-extinstall>
        <OOFUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</OOFUrl>
        <UMUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/UM2007Legacy.asmx</UMUrl>
        <OABUrl>https://dummy.CUSTOM_DOMAINowa.com/OAB/64a3cd6e-8eaf-4df2-8732-a2e75bbf7c6a/</OABUrl>
        <ServerExclusiveConnect>off</ServerExclusiveConnect>
      </Protocol>
      <Protocol>
        <Type>EXPR</Type>
        <Server>dummy.CUSTOM_DOMAINowa.com</Server>
        <SSL>On</SSL>
        <AuthPackage>Ntlm</AuthPackage>
        <ASUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</ASUrl>
        <EwsUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</EwsUrl>
        <EmwsUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</EmwsUrl>
        <EcpUrl>https://dummy.CUSTOM_DOMAINowa.com/ecp/</EcpUrl>
        <EcpUrl-um>?rfr=olk&amp;p=customize/voicemail.aspx&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-um>
        <EcpUrl-aggr>?rfr=olk&amp;p=personalsettings/EmailSubscriptions.slab&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-aggr>
        <EcpUrl-mt>PersonalSettings/DeliveryReport.aspx?rfr=olk&amp;exsvurl=1&amp;IsOWA=&lt;IsOWA&gt;&amp;MsgID=&lt;MsgID&gt;&amp;Mbx=&lt;Mbx&gt;&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-mt>
        <EcpUrl-ret>?rfr=olk&amp;p=organize/retentionpolicytags.slab&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-ret>
        <EcpUrl-sms>?rfr=olk&amp;p=sms/textmessaging.slab&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-sms>
        <EcpUrl-photo>PersonalSettings/EditAccount.aspx?rfr=olk&amp;chgPhoto=1&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-photo>
        <EcpUrl-tm>?rfr=olk&amp;ftr=TeamMailbox&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-tm>
        <EcpUrl-tmCreating>?rfr=olk&amp;ftr=TeamMailboxCreating&amp;SPUrl=&lt;SPUrl&gt;&amp;Title=&lt;Title&gt;&amp;SPTMAppUrl=&lt;SPTMAppUrl&gt;&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-tmCreating>
        <EcpUrl-tmEditing>?rfr=olk&amp;ftr=TeamMailboxEditing&amp;Id=&lt;Id&gt;&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-tmEditing>
        <EcpUrl-extinstall>Extension/InstalledExtensions.slab?rfr=olk&amp;exsvurl=1&amp;realm=CSTDOM.CUSTOM_DOMAINopg.com</EcpUrl-extinstall>
        <OOFUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</OOFUrl>
        <UMUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/UM2007Legacy.asmx</UMUrl>
        <OABUrl>https://dummy.CUSTOM_DOMAINowa.com/OAB/64a3cd6e-8eaf-4df2-8732-a2e75bbf7c6a/</OABUrl>
        <ServerExclusiveConnect>on</ServerExclusiveConnect>
        <EwsPartnerUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</EwsPartnerUrl>
        <GroupingInformation>SAP01</GroupingInformation>
      </Protocol>
      <Protocol>
        <Type>WEB</Type>
        <Internal>
          <OWAUrl AuthenticationMethod="Basic, Fba">https://dummy.CUSTOM_DOMAINowa.com/owa/</OWAUrl>
          <Protocol>
            <Type>EXCH</Type>
            <ASUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</ASUrl>
          </Protocol>
        </Internal>
        <External>
          <OWAUrl AuthenticationMethod="Fba">https://dummy.CUSTOM_DOMAINowa.com/owa/</OWAUrl>
          <Protocol>
            <Type>EXPR</Type>
            <ASUrl>https://dummy.CUSTOM_DOMAINowa.com/EWS/Exchange.asmx</ASUrl>
          </Protocol>
        </External>
      </Protocol>
      <PublicFolderInformation>
        <SmtpAddress>dummy1pf@CUSTOM_DOMAIN.com</SmtpAddress>
      </PublicFolderInformation>
    </Account>
  </Response>
</Autodiscover>
</Trace>

EWS Trace logs :<Trace Tag="EwsRequest" Tid="1" Time="2016-01-12 23:42:34Z">
<?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"></t:RequestServerVersion></soap:Header><soap:Body><m:FindItem Traversal="Shallow"><m:ItemShape><t:BaseShape>AllProperties</t:BaseShape></m:ItemShape><m:CalendarView StartDate="2016-01-12T23:42:34Z" EndDate="2016-01-17T23:42:34Z"></m:CalendarView><m:ParentFolderIds><t:DistinguishedFolderId Id="calendar"></t:DistinguishedFolderId></m:ParentFolderIds></m:FindItem></soap:Body></soap:Envelope>
</Trace>

microsoft.exchange.webservices.data.exception.ServiceRequestException: The request failed. The request failed. The remote server returned an error: (403)Forbidden ( The server denied the specified Uniform Resource Locator (URL). Contact the server administrator.  )
    at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:76)
    at microsoft.exchange.webservices.data.core.request.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:158)
    at microsoft.exchange.webservices.data.core.ExchangeService.findItems(ExchangeService.java:980)
    at microsoft.exchange.webservices.data.core.ExchangeService.findAppointments(ExchangeService.java:1259)
    at microsoft.exchange.webservices.data.core.ExchangeService.findAppointments(ExchangeService.java:1280)
    at CustomExchangeJavaService.getFewAppointments(Prototype_EWS.java:272)
    at Prototype_EWS.main(Prototype_EWS.java:152)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: microsoft.exchange.webservices.data.exception.ServiceRequestException: The request failed. The remote server returned an error: (403)Forbidden ( The server denied the specified Uniform Resource Locator (URL). Contact the server administrator.  )
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:716)
    at microsoft.exchange.webservices.data.core.request.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:64)
    ... 11 more
Caused by: microsoft.exchange.webservices.data.exception.HttpErrorException: The remote server returned an error: (403)Forbidden ( The server denied the specified Uniform Resource Locator (URL). Contact the server administrator.  )
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:776)
    at microsoft.exchange.webservices.data.core.request.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:711)
    ... 12 more
java.lang.NullPointerException
serious6 commented 8 years ago

possible ref. #475? start investigation with the suggested tools.

pheenomenon commented 8 years ago

@serious6 Thank you for your reply. Unfortunately #475 does not help. I was using WebCredentials(user, pass); earlier, even with WebCredentials(user, pass, domain); I get the same error.

ews-editor does the same stuff what my code does. I see the below XML response for DoAutodiscover but TestExchangeService fails.

016-01-13T16:22:53|EwsTrace|DoAutodiscover|AutodiscoverResponse|
<Trace Tag="AutodiscoverResponse" Tid="1" Time="2016-01-14 00:22:53Z" Version="15.00.0913.015">
  <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing">
    <s:Header>
      <a:Action s:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetUserSettingsResponse</a:Action>
      <h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/2010/Autodiscover" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <h:MajorVersion>15</h:MajorVersion>
        <h:MinorVersion>0</h:MinorVersion>
        <h:MajorBuildNumber>1104</h:MajorBuildNumber>
        <h:MinorBuildNumber>0</h:MinorBuildNumber>
        <h:Version>Exchange2013_SP1</h:Version>
      </h:ServerVersionInfo>
    </s:Header>
    <s:Body>
      <GetUserSettingsResponseMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover">
        <Response xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
          <ErrorCode>NoError</ErrorCode>
          <ErrorMessage />
          <UserResponses>
            <UserResponse>
              <ErrorCode>NoError</ErrorCode>
              <ErrorMessage>No error.</ErrorMessage>
              <RedirectTarget i:nil="true" />
              <UserSettingErrors />
              <UserSettings>
                <UserSetting i:type="StringSetting">
                  <Name>InternalEwsUrl</Name>
                  <Value>https://dummy.domainowa.com/EWS/Exchange.asmx</Value>
                </UserSetting>
                <UserSetting i:type="StringSetting">
                  <Name>ExternalEwsUrl</Name>
                  <Value>https://dummy.domainowa.com/EWS/Exchange.asmx</Value>
                </UserSetting>
              </UserSettings>
            </UserResponse>
          </UserResponses>
        </Response>
      </GetUserSettingsResponseMessage>
    </s:Body>
  </s:Envelope>
</Trace>

2016-01-13T16:22:53|EwsTrace|TestExchangeService|EwsRequestHttpHeaders|
<Trace Tag="EwsRequestHttpHeaders" Tid="1" Time="2016-01-14 00:22:53Z">
POST /EWS/Exchange.asmx HTTP/1.1
Content-Type: text/xml; charset=utf-8
Accept: text/xml
User-Agent: EwsEditor (ExchangeServicesClient/15.00.0913.015)
Accept-Encoding: gzip,deflate

</Trace>

2016-01-13T16:22:53|EwsTrace|TestExchangeService|EwsRequest|
<Trace Tag="EwsRequest" Tid="1" Time="2016-01-14 00:22:53Z" Version="15.00.0913.015">
  <?xml version="1.0" encoding="utf-8"?>
  <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" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header>
      <t:RequestServerVersion Version="Exchange2013_SP1" />
    </soap:Header>
    <soap:Body>
      <m:ConvertId DestinationFormat="HexEntryId">
        <m:SourceIds>
          <t:AlternateId Format="HexEntryId" Id="00" Mailbox="blah@blah.com" />
        </m:SourceIds>
      </m:ConvertId>
    </soap:Body>
  </soap:Envelope>
</Trace>

2016-01-13T16:22:53|EwsTrace|TestExchangeService|EwsResponseHttpHeaders|
<Trace Tag="EwsResponseHttpHeaders" Tid="1" Time="2016-01-14 00:22:53Z">
HTTP/1.1 403 Forbidden ( The server denied the specified Uniform Resource Locator (URL). Contact the server administrator.  )
Connection: close
Pragma: no-cache
Content-Length: 2040
Cache-Control: no-cache
Content-Type: text/html

</Trace>

2016-01-13T16:22:53|Error|Application_ThreadException|Unhandled exception!|
Exception details:
Message: The request failed. The remote server returned an error: (403) Forbidden.
Type: Microsoft.Exchange.WebServices.Data.ServiceRequestException
Source: Microsoft.Exchange.WebServices
Stack Trace:
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(IEwsHttpWebRequest& request)
   at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
   at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalConvertIds(IEnumerable`1 ids, IdFormat destinationFormat, ServiceErrorHandling errorHandling)
   at Microsoft.Exchange.WebServices.Data.ExchangeService.ConvertIds(IEnumerable`1 ids, IdFormat destinationFormat)
   at EWSEditor.Common.Extensions.ExchangeServiceExtensions.TestExchangeService(ExchangeService service) in e:\msft tools\ewseditor\EWSEditor\Common\Extensions\ExchangeServiceExtensions.cs:line 171
   at EWSEditor.Forms.ServiceDialog.BtnOK_Click(Object sender, EventArgs e) in e:\msft tools\ewseditor\EWSEditor\Forms\Dialogs\ServiceDialog.cs:line 148
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Exception details:
Message: The remote server returned an error: (403) Forbidden.
Type: System.Net.WebException
Source: System
Stack Trace:
   at System.Net.HttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.EwsHttpWebRequest.Microsoft.Exchange.WebServices.Data.IEwsHttpWebRequest.GetResponse()
   at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.GetEwsHttpWebResponse(IEwsHttpWebRequest request)