Results 1 to 9 of 9

Thread: Add account using SOAP

  1. #1
    majodo is offline Senior Member
    Join Date
    Mar 2007
    Posts
    55
    Rep Power
    8

    Default Add account using SOAP

    Hello ,
    Has someone already did an application that add zimbra account using SOAP
    thx

  2. #2
    mrojek is offline Member
    Join Date
    May 2007
    Location
    Poland Warszawa
    Posts
    11
    Rep Power
    8

    Default Soap accont create

    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

  3. #3
    majodo is offline Senior Member
    Join Date
    Mar 2007
    Posts
    55
    Rep Power
    8

    Default

    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

  4. #4
    mrojek is offline Member
    Join Date
    May 2007
    Location
    Poland Warszawa
    Posts
    11
    Rep Power
    8

    Default

    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:
    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());
    3. If all go ok on you console show samethig like that:
    Code:
    <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>
    4. You must change bold element in code

    5. It should work - sorry for my English I still learn this language

  5. #5
    majodo is offline Senior Member
    Join Date
    Mar 2007
    Posts
    55
    Rep Power
    8

    Default

    Thanks also for your code

  6. #6
    ozomozo is offline Junior Member
    Join Date
    Sep 2007
    Posts
    9
    Rep Power
    7

    Default

    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());
    }
    __________________________________________________ ___________________________
    Last edited by ozomozo; 10-05-2007 at 04:41 PM.

  7. #7
    dkarp is offline Zimbra Employee
    Join Date
    Aug 2005
    Posts
    1,433
    Rep Power
    11

    Default

    Much easier is to use the createAccount() method in the com.zimbra.cs.account.soap.SoapProvisioning.java class provided with the ZCS source.
    Bugzilla - Wiki - Downloads - Before posting... Search!

  8. #8
    ozomozo is offline Junior Member
    Join Date
    Sep 2007
    Posts
    9
    Rep Power
    7

    Default

    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)
    Last edited by ozomozo; 10-08-2007 at 10:51 AM.

  9. #9
    tharaka is offline Starter Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    2

    Default Jar

    Quote Originally Posted by mrojek View Post
    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:
    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());
    3. If all go ok on you console show samethig like that:
    Code:
    <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>
    4. You must change bold element in code

    5. It should work - sorry for my English I still learn this language

    Please let me know the place to download the relevant jar files. I searched more hours for jars but didn't get yet. So I am stuck on that.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 8
    Last Post: 04-21-2007, 10:29 PM
  2. Replies: 3
    Last Post: 03-04-2007, 10:53 PM
  3. Add New User account using MySql
    By tangtan in forum Developers
    Replies: 3
    Last Post: 11-04-2006, 09:28 AM
  4. Error when adding account : cannot add alias
    By Bingo in forum Administrators
    Replies: 5
    Last Post: 06-21-2006, 03:32 AM
  5. Using Zimlet to add zimbra account
    By dzed in forum Zimlets
    Replies: 2
    Last Post: 05-05-2006, 02:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •