Zimbra SOAP API authenticate using zm_auth_token in java
I开发者_如何学JAVA want to authenticate a user using the zm_auth_token that I dispose :
For the moment, I'm doing this :
LmcAuthRequest auth = new LmcAuthRequest();
auth.setUsername(userName);
auth.setPassword(password);
LmcAuthResponse authResp = (LmcAuthResponse) auth.invoke(serverURL);
LmcSession session = authResp.getSession();
But I want to use the zm_auth_token that I have. How to do this ??? Thnx
The zimbra Lmc methods are deprecated now ... If you want to use SOAP they prefer doing it using ZMailBox (It doesn't work for me), I used this method :
// Create the connection where we're going to send the file.
URL url = new URL(SOAPUrl);
URLConnection connection = url.openConnection();
HttpURLConnection httpConn = (HttpURLConnection) connection;
String postContent = "<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\">"+
"<soap:Header>" +
"<context xmlns=\"urn:zimbra\">" +
"<format type=\"js\"/>" +
"<authToken>" + authToken + "</authToken>" +
"</context>" +
"</soap:Header>" +
"<soap:Body>" +
"<GetFolderRequest xmlns=\"urn:zimbraMail\" />" +
"</soap:Body>" +
"</soap:Envelope>";
// insert your SOAP XML!!!
byte[] b = postContent.getBytes();
// Set the appropriate HTTP parameters.
httpConn.setRequestProperty( "Content-Length", String.valueOf( b.length ) );
httpConn.setRequestProperty("Content-Type","application/soap+xml; charset=utf-8");
httpConn.setRequestMethod( "POST" );
httpConn.setDoOutput(true);
httpConn.setDoInput(true);
// Everything's set up; send the XML that was read in to b.
OutputStream out = httpConn.getOutputStream();
out.write( b );
out.close();
// Read the response and write it to standard out.
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
// read & do something with input stream...
String s = null;
String response = "";
while((s=in.readLine()) != null){
response += s;
}
in.close();
SOAPConnectionFactory soapfactory=SOAPConnectionFactory.newInstance();
SOAPConnection soapconnection=soapfactory.createConnection();
MessageFactory messagefactory=MessageFactory.newInstance();
SOAPMessage messege=messagefactory.createMessage();
SOAPEnvelope envelop=messege.getSOAPPart().getEnvelope();
SOAPHeader header=messege.getSOAPHeader();
SOAPBody body=messege.getSOAPBody();
Name header_context=envelop.createName("context", null,"urn:zimbra");
Name auth_request=envelop.createName("AuthRequest",null,"urn:zimbraAccount");
Name account=envelop.createName("account");
Name password=envelop.createName("password");
header.addHeaderElement(header_context);
SOAPBodyElement auth_body=body.addBodyElement(auth_request);
auth_body.addChildElement(account).addAttribute(envelop.createName("by"),"name").addTextNode("abc");//(abc==your username)
auth_body.addChildElement(password).addTextNode("1234");//(1234=your password)
URL url=new URL("http://192.168.1.67/service/soap/AuthRequest");
SOAPMessage response=soapconnection.call(messege, url);
You can use Zimbra libraries for calling SOAP API. Please check this answer.
精彩评论