This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Web Services and the fly likes EWS : Transport error: 401 Error: Unauthorized Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "EWS : Transport error: 401 Error: Unauthorized" Watch "EWS : Transport error: 401 Error: Unauthorized" New topic
Author

EWS : Transport error: 401 Error: Unauthorized

dwarakanathan thiru
Ranch Hand

Joined: Oct 14, 2009
Posts: 49
Hi,

I get the following Exception when trying to access a EWS Service from a java client using JAX-RPC:

Nov 5, 2009 5:53:34 PM org.apache.commons.httpclient.HttpMethodDirector authenticateHost
WARNING: Required credentials not available for BASIC <any realm>@abc.def.xyz:443
Nov 5, 2009 5:53:34 PM org.apache.commons.httpclient.HttpMethodDirector authenticateHost
WARNING: Preemptive authentication requested but no default credentials available
Nov 5, 2009 5:53:36 PM org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme
INFO: NTLM authentication scheme selected
Nov 5, 2009 5:53:36 PM org.apache.commons.httpclient.HttpMethodDirector processWWWAuthChallenge
INFO: No credentials available for NTLM <any realm>@abc.def.xyz:443
Nov 5, 2009 5:53:36 PM org.apache.axis2.transport.http.HTTPSender sendViaPost
INFO: Unable to sendViaPost to url[https://abc.def.xyz/EWS/Exchange.asmx]
org.apache.axis2.AxisFault: Transport error: 401 Error: Unauthorized
at org.apache.axis2.transport.http.HTTPSender.handleResponse(HTTPSender.java:295)
at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:190)
at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:389)
at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:222)
at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:435)
at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:402)
at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229)
at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165)
at com.microsoft.schemas.exchange.services._2006.messages.ExchangeWebServiceStub.FindFolder(ExchangeWebServiceStub.java:8768)
at com.client.ExchangeClient.findFolder(ExchangeClient.java:208)
at com.client.ExchangeClient.execute(ExchangeClient.java:160)
at com.client.ExchangeClient.main(ExchangeClient.java:52)

This is the source cde:

public class ExchangeClient {

public void execute() {

...
try {
/***** Keystore Setting *****/

System.setProperty("javax.net.ssl.trustStore", "key.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "pwd");
// This is the STUB generated using AXIS WSDL 2 JAVA //
ExchangeWebServiceStub serviceStub = new ExchangeWebServiceStub(wsdlURL);
/***** Setting Trust Issues *****/
TrustManager[] trustAllCerts = new TrustManager[] { trustAllCerts() };
sslContextGenerator(trustAllCerts);

/***** Setting Authenticator *****/
List authScheme = new ArrayList();
authScheme.add(Authenticator.NTLM);
Authenticator authenticator = new Authenticator();
authenticator.setAuthSchemes(authScheme);
authenticator.setUsername("aaa");
authenticator.setPassword("eee");
authenticator.setHost("HOST");
authenticator.setDomain("DOMAIN");
authenticator.setPort(443);
authenticator.setAllowedRetry(true);
authenticator.setPreemptiveAuthentication(true);

/***** Setting Options *****/
Options options = serviceStub._getServiceClient().getOptions();
options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
options.setProperty(org.apache.axis2.transport.http.HTTPConstants.CHUNKED, Boolean.FALSE);
options.setTransportInProtocol("https");
options.setTo(new EndpointReference("https://abc.def.com/EWS/Exchange.asmx"));
serviceStub._getServiceClient().setOptions(options);

/***********************/
//Find folder
findFolder(serviceStub);
} catch (Exception e) {
e.printStackTrace();
}
}

private void findFolder(ExchangeWebServiceStub serviceStub)
throws RemoteException {
// SerializedSecurityContext
SerializedSecurityContext ssc = new SerializedSecurityContext();
SerializedSecurityContextType param3 = new SerializedSecurityContextType();
param3.setUserSid("aaa");
ssc.setSerializedSecurityContext(param3);

// MailboxCulture
MailboxCulture mbc = new MailboxCulture();
Language param4 = new Language();
mbc.setMailboxCulture(param4);

FindFolder find = new FindFolder();
FindFolderType fft = new FindFolderType();
FolderQueryTraversalType fqt = new FolderQueryTraversalType("", true);
fft.setTraversal(fqt);
FolderResponseShapeType frst = new FolderResponseShapeType();
frst.setBaseShape(DefaultShapeNamesType.AllProperties);
fft.setFolderShape(frst);
NonEmptyArrayOfBaseFolderIdsType neaobft = new NonEmptyArrayOfBaseFolderIdsType();
NonEmptyArrayOfBaseFolderIdsTypeChoice[] param = new NonEmptyArrayOfBaseFolderIdsTypeChoice [1];
param[0] = new NonEmptyArrayOfBaseFolderIdsTypeChoice();
neaobft.setNonEmptyArrayOfBaseFolderIdsTypeChoice(param);
fft.setParentFolderIds(neaobft);
find.setFindFolder(fft);

// ExchangeImpersonation
ExchangeImpersonation ei = new ExchangeImpersonation();
ExchangeImpersonationType param2 = new ExchangeImpersonationType();
ConnectingSIDType sid = new ConnectingSIDType();
param2.setConnectingSID(sid);
ei.setExchangeImpersonation(param2);

RequestServerVersion rsv = new RequestServerVersion();
rsv.setVersion(ExchangeVersionType.Exchange2007_SP1);

serviceStub.FindFolder(find, ei, ssc, mbc, rsv);
}


private static void sslContextGenerator(TrustManager[] trustAllCerts) {
try {
SSLContext sc;
sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());

// For certificate issues
HttpsURLConnection
.setDefaultSSLSocketFactory(sc.getSocketFactory());
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
} catch (KeyManagementException e1) {
e1.printStackTrace();
}
HttpsURLConnection.setFollowRedirects(false);
}


private static X509TrustManager trustAllCerts() {
return new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(final X509Certificate[] certs,
final String authType) {

}

public void checkServerTrusted(
final java.security.cert.X509Certificate[] certs,
final String authType) {

}
};
}
}

I have very less idea why this is happening. The credentials are correct. Please let me know your thoughts on the same.


Thanks,
Dwarak T
Pat Gonzalez
Greenhorn

Joined: Oct 18, 2009
Posts: 19
It appears that it did not accept your authentication scheme (Basic Auth).

If the web service supports integrated windows authentication or kerberos, you should be able to use the client from this open source library...

http://spnego.sourceforge.net/protected_soap_service.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EWS : Transport error: 401 Error: Unauthorized