Hello ,
Has someone already did an application that add zimbra account using SOAP
thx
Printable View
Hello ,
Has someone already did an application that add zimbra account using SOAP
thx
Yes i write simple java aplication witch via soap add account. You must login on ZImbraServer:7071/service/admin/soap
and send CreateAccount request witch is describe in soap-admin.txt
hello thx for your answer
it will very nice from you if u sent me your code java because i already try what u say and i have some exception that seams difficult for me
thx
Ok this is my code mybe it not look pretty but it work:
1. First you must connect to zimbra admin SOAP service for example and get auth token and session ID:
Code:import com.zimbra.common.service.ServiceException;
import com.zimbra.cs.service.account.AccountService;
import com.zimbra.cs.service.admin.AdminService;
import com.zimbra.soap.Element;
import com.zimbra.soap.SoapFaultException;
import com.zimbra.soap.SoapHttpTransport;
import com.zimbra.soap.ZimbraSoapContext;
Code:SoapHttpTransport trans = null;
try {
trans = new SoapHttpTransport("https://youZimbraServer:7071/service/admin/soap/"
);
Element request = Element.XMLElement.mFactory .createElement(AdminService.AUTH_REQUEST);
request.addAttribute(AdminService.E_NAME,"admin@example.com",Element.DISP_CONTENT);
request.addAttribute(AdminService.E_PASSWORD, "password",
Element.DISP_CONTENT);
response = trans.invoke(request);
} catch (Exception e) {
e.printStackTrace();
}
String authToken = null;
// get the auth token out, no default, must be present or a service exception is thrown
try {
authToken = response.getAttribute(AccountService.E_AUTH_TOKEN);
} catch (ServiceException e) {
//
e.printStackTrace();
}
// get the session id, if not present, default to null
String sessionId = response.getAttribute(ZimbraSoapContext.E_SESSION_ID, null);
System.out.println(sessionId);
trans.setAuthToken(authToken);
if (sessionId != null)
trans.setSessionId(sessionId);
2. Now when you have a token and session ID you can send a request to create account:
3. If all go ok on you console show samethig like that:Code:Element accRequest = Element.XMLElement.mFactory.createElement(AdminService.CREATE_ACCOUNT_REQUEST);
accRequest.addElement(AdminService.E_NAME).setText("test@example.com");
accRequest.addElement(AdminService.E_PASSWORD).setText("examplepasswd");
//You can set another parametr for acconut for example:
//tagsRequest.addElement(AdminService.E_A).addAttribute("n", "zimbraPrefFromDisplay").setText("example");
//in response you get a all atribute for account
Element accResponse = null;
try {
accResponse = trans.invoke(accRequest);
} catch (SoapFaultException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(accResponse.prettyPrint());
4. You must change bold element in codeCode:<CreateAccountResponse xmlns="urn:zimbraAdmin">
<account id="zimbra id" name="test@example.com">
<a n="zimbraPrefGalAutoCompleteEnabled">FALSE</a>
<a n="zimbraPrefGroupMailBy">conversation</a>
....another atribute for account
....
<a n="zimbraPrefCalendarInitialView">workWeek</a>
</account>
</CreateAccountResponse>
5. It should work - sorry for my English I still learn this language
Thanks also for your code
mrojek ... thanks for this post. I tried all that you said and i get this error:
at ZimbraSoap.main(ZimbraSoap.java:30)
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.httpclient.methods.PostMethod.s etRequestContentLength(J)V
at com.zimbra.soap.SoapHttpTransport.invoke(SoapHttpT ransport.java:184)
at com.zimbra.soap.SoapTransport.invoke(SoapTransport .java:231)
at ZimbraSoap.createUser(ZimbraSoap.java:110)
at ZimbraSoap.main(ZimbraSoap.java:30)
My code is straightforward:
__________________________________________________ ________________________
public void createUser() {
SoapHttpTransport trans = null;
try {
trans = new SoapHttpTransport(
"https://localhost.localdomain:7071/service/admin/soap/");
request = Element.XMLElement.mFactory
.createElement(AdminService.AUTH_REQUEST);
request.addAttribute(AdminService.E_NAME, "admin",
Element.DISP_CONTENT);
request.addAttribute(AdminService.E_PASSWORD, "password",
Element.DISP_CONTENT);
response = trans.invoke(request);
} catch (Exception e) {
e.printStackTrace();
} finally {
}
String authToken = null;
try {
authToken = response.getAttribute(AccountService.E_AUTH_TOKEN) ;
} catch (ServiceException e) {
e.printStackTrace();
}
String sessionId = response.getAttribute(
ZimbraSoapContext.E_SESSION_ID, null);
System.out.println(sessionId);
trans.setAuthToken(authToken);
if (sessionId != null)
trans.setSessionId(sessionId);
Element accRequest = Element.XMLElement.mFactory
.createElement(AdminService.CREATE_ACCOUNT_REQUEST );
accRequest.addElement(AdminService.E_NAME).setText ("test@example.com");
accRequest.addElement(AdminService.E_PASSWORD).set Text("examplepasswd");
Element accResponse = null;
try {
accResponse = trans.invoke(accRequest);
} catch (SoapFaultException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(accResponse.prettyPrint());
}
__________________________________________________ ___________________________
Much easier is to use the createAccount() method in the com.zimbra.cs.account.soap.SoapProvisioning.java class provided with the ZCS source.
dkarp
I tried using createAccount() and its the same:
__________________________________________________ ______________-
static SoapProvisioning sp = new SoapProvisioning();
private Account createAccount;
public void createAccount() {
try {
sp.soapSetURI("https://localhost.localdomain:7071" + ZimbraServlet.ADMIN_SERVICE_URI);
sp.soapAdminAuthenticate("admin","password");
createAccount = sp.createAccount("name@address.com", "zimbra", getMap());
}
catch(ServiceException se) {
se.printStackTrace();
} finally {
}
}
__________________________________________________ ______________
Error Stack:
at org.apache.commons.logging.LogFactory.getLog(LogFa ctory.java:353)
at org.apache.commons.httpclient.HttpClient.<clinit>( HttpClient.java:69)
at com.zimbra.soap.SoapHttpTransport.<init>(SoapHttpT ransport.java:71)
at com.zimbra.cs.account.soap.SoapProvisioning.soapSe tURI(SoapProvisioning.java:88)
at ZimbraSoap.createAccount(ZimbraSoap.java:110)
at ZimbraSoap.main(ZimbraSoap.java:38)