Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to authenticate EWS Java API

We are using EWS Java API to use the outlook calendar on our Java application. I am having authentication issues on EWS.

I tried the application on the cloud outlook account that's supplied by rackspace and everything worked just fine so I know the credentials are accurate.

Here is the code:

import java.net.URI;
import java.net.URISyntaxException;
import microsoft.exchange.webservices.data.*;

public class TestClass {

    public static void main(String[] args) {
        TestClass obj = new TestClass();
        obj.testMethod();
    }

    public void testMethod() {
        ExchangeService service = new ExchangeService(
                ExchangeVersion.Exchange2007_SP1);
        ExchangeCredentials credentials = new WebCredentials("username",
                "password");

        service.setCredentials(credentials);

        try {
            service.setUrl(new URI("https://domain/EWS/Exchange.asmx"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        EmailMessage msg;
        try {
            msg = new EmailMessage(service);
            msg.setSubject("hello world");
            msg.setBody(MessageBody
                    .getMessageBodyFromText("Sent using the EWS API"));
            msg.getToRecipients().add("[email protected]");
            msg.send();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

The url for rackspace is:https://connect.emailsrvr.com/EWS/Exchange.asmx When I put the username and password for this account and it works, I see the console spitting out this one:

Apr 05, 2013 1:40:28 PM org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: NTLM authentication scheme selected

Our client is using ExchangeVersion.Exchange2007_SP1 whereas Rackspace is using ExchangeVersion.Exchange2010 But when I use the credentials(username, password and url) that our client provided, I am getting this error:

Apr 05, 2013 1:49:13 PM org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: Basic authentication scheme selected
Apr 05, 2013 1:49:13 PM org.apache.commons.httpclient.HttpMethodDirector processAuthenticationResponse
SEVERE: Invalid challenge: Basic
org.apache.commons.httpclient.auth.MalformedChallengeException: Invalid challenge: Basic
at org.apache.commons.httpclient.auth.AuthChallengeParser.extractParams(AuthChallengeParser.java:98)
at org.apache.commons.httpclient.auth.RFC2617Scheme.processChallenge(RFC2617Scheme.java:94)
at org.apache.commons.httpclient.auth.BasicScheme.processChallenge(BasicScheme.java:112)
at org.apache.commons.httpclient.auth.AuthChallengeProcessor.processChallenge(AuthChallengeProcessor.java:162)
at org.apache.commons.httpclient.HttpMethodDirector.processWWWAuthChallenge(HttpMethodDirector.java:694)
at org.apache.commons.httpclient.HttpMethodDirector.processAuthenticationResponse(HttpMethodDirector.java:668)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:193)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at microsoft.exchange.webservices.data.HttpClientWebRequest.executeRequest(HttpClientWebRequest.java:358)
at microsoft.exchange.webservices.data.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:930)
at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:825)
at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:46)
at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:144)
at microsoft.exchange.webservices.data.ExchangeService.internalCreateItems(ExchangeService.java:464)
at microsoft.exchange.webservices.data.ExchangeService.createItem(ExchangeService.java:535)
at microsoft.exchange.webservices.data.Item.internalCreate(Item.java:215)
at microsoft.exchange.webservices.data.EmailMessage.internalSend(EmailMessage.java:125)
at microsoft.exchange.webservices.data.EmailMessage.send(EmailMessage.java:253)
at com.aurora.trials.TestClass.testMethod(TestClass.java:43)
at com.aurora.trials.TestClass.main(TestClass.java:17)

microsoft.exchange.webservices.data.EWSHttpException: Connection not established
at microsoft.exchange.webservices.data.HttpClientWebRequest.throwIfConnIsNull(HttpClientWebRequest.java:394)
at microsoft.exchange.webservices.data.HttpClientWebRequest.getResponseHeaders(HttpClientWebRequest.java:280)
at microsoft.exchange.webservices.data.ExchangeServiceBase.processHttpResponseHeaders(ExchangeServiceBase.java:1045)
at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:58)
at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:144)
at microsoft.exchange.webservices.data.ExchangeService.internalCreateItems(ExchangeService.java:464)
at microsoft.exchange.webservices.data.ExchangeService.createItem(ExchangeService.java:535)
at microsoft.exchange.webservices.data.Item.internalCreate(Item.java:215)
at microsoft.exchange.webservices.data.EmailMessage.internalSend(EmailMessage.java:125)
at microsoft.exchange.webservices.data.EmailMessage.send(EmailMessage.java:253)
at com.aurora.trials.TestClass.testMethod(TestClass.java:43)
at com.aurora.trials.TestClass.main(TestClass.java:17)

I couldn't find any solution for this issue. Please provide any info that you think I can get authenticated for EWS. What is causing these Exceptions?

like image 541
leventgo Avatar asked Apr 05 '13 19:04

leventgo


People also ask

What authentication does EWS use?

Basic authentication is no longer supported for EWS to connect to Exchange Online. Use OAuth authentication in all your new or existing EWS applications to connect to Exchange Online. OAuth authentication for EWS is only available in Exchange Online as part of Microsoft 365.

How do I enable basic authentication on EWS?

On the Exchange Server hosting the Exchange Web Services open the Internet Information Services (IIS) Manager administrative tool. Navigate through to Server | Sites | Default Web Site | EWS. Select the Authentication icon from the feature view. Ensure that Basic Authentication is enabled.

How do I enable OAuth in EWS?

To use OAuth with your application you will need to: Register your application with Azure Active Directory. Add code to get an authentication token to get an authentication token from a token server. Add an authentication token to EWS requests that you send.


1 Answers

I had your exact same errors and wasted 5 hours trying to fix it. My conclusion is that you should not waste more time on trying to get the EWS codebase to work, the EWS code is confused and you'll have to go in under the hood to fix bugs in that library.

I settled on using javax.mail implementation which does what I need. https://stackoverflow.com/a/18043717/445131

like image 158
Eric Leschinski Avatar answered Sep 21 '22 17:09

Eric Leschinski