How to generate the authtoken with Java?

Have a great idea for extending Zimbra? Share ideas, ask questions, contribute, and get feedback.
Post Reply
Rafael
Posts: 11
Joined: Tue Sep 06, 2016 2:49 pm

How to generate the authtoken with Java?

Post by Rafael »

Hi,

Someone know how generate the authtoken using java?

I Tried use this code:

AuthRequest authRequest = new AuthRequest();
AuthResponse authResponse = new AuthResponse();
AccountSelector accountSelector = new AccountSelector();
AuthToken auth = new AuthToken();


//authRequest.setAuthToken(auth);

accountSelector.setBy(AccountBy.name);
accountSelector.set_value("rafael.santos@service.com.br");

authRequest.setAccount(accountSelector);
authRequest.setPersistAuthTokenCookie(true);
authRequest.setPassword("Rafa1208");

String resp = authResponse.getAuthToken();

But this return null.

What I must to do? Someone have a example of code to I see how it work?

Best regards
Rafael
thorx
Advanced member
Advanced member
Posts: 54
Joined: Sat Sep 13, 2014 3:26 am

Re: How to generate the authtoken with Java?

Post by thorx »

no idea how you do it in java, but I posted some of the soap structure here:
viewtopic.php?f=22&t=60227&p=270501#p270501

I just use normal http client and post soap request using that. Done that usin powershell and python with great success.
G71
Posts: 23
Joined: Fri Aug 19, 2016 10:01 am

Re: How to generate the authtoken with Java?

Post by G71 »

Rafael wrote:Hi,

Someone know how generate the authtoken using java?

I Tried use this code:

AuthRequest authRequest = new AuthRequest();
AuthResponse authResponse = new AuthResponse();
AccountSelector accountSelector = new AccountSelector();
AuthToken auth = new AuthToken();


//authRequest.setAuthToken(auth);

accountSelector.setBy(AccountBy.name);
accountSelector.set_value("rafael.santos@service.com.br");

authRequest.setAccount(accountSelector);
authRequest.setPersistAuthTokenCookie(true);
authRequest.setPassword("Rafa1208");

String resp = authResponse.getAuthToken();

But this return null.

What I must to do? Someone have a example of code to I see how it work?

Best regards
Rafael
Dear, I'm assuming that you are using some lib axis2 like.

Try to study the implementation with java.xml

Code: Select all

private String getToken(){
		String token="";
		try {

		MessageFactory messagefactory;
		SOAPMessage message;
		messagefactory=MessageFactory.newInstance();
		message=messagefactory.createMessage();

		SOAPConnectionFactory soapfactory;
		soapfactory=SOAPConnectionFactory.newInstance();
		SOAPConnection soapconnection;
		soapconnection=soapfactory.createConnection();
		SOAPHeader header;
		header=message.getSOAPHeader();
		SOAPBody body;	
		body=message.getSOAPBody();
		SOAPEnvelope envelop;
		envelop=message.getSOAPPart().getEnvelope();
		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("test@zimbramail.br");//(test@zimbramail.br==your username)
		auth_body.addChildElement(password).addTextNode("12345678");//(12345678=your password)
		URL url;
			url = new URL("https://yoururl:443/service/soap/");
			SOAPMessage response=soapconnection.call(message, url);	
			
			SOAPPart sp = response.getSOAPPart();
		    SOAPEnvelope se = sp.getEnvelope();
		    SOAPBody sb = se.getBody();
		    Iterator it = sb.getChildElements();
		    while (it.hasNext()) {
		        SOAPBodyElement bodyElement = (SOAPBodyElement) it.next();

			    System.out.println("bodyElement " + bodyElement.getNodeName());
		        Iterator it4 = bodyElement.getChildElements();
		        while (it4.hasNext()) {
		            SOAPElement element2 = (SOAPElement) it4.next();
				    if("authToken".equalsIgnoreCase(element2.getNodeName())){

				    	token= element2.getValue();

					    System.out.println("token " + token);
				    }
		        }
		    }
		    
		    
		    
		} catch (SOAPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return token;
	
	}
Rafael
Posts: 11
Joined: Tue Sep 06, 2016 2:49 pm

Re: How to generate the authtoken with Java?

Post by Rafael »

Hi G71,

Thank you for the help again.

I never used the axis2 before, is the first time, and I don't know well how it work.

But I Use the code that you sent to me, I write line to line, to understand better what I'm doing, but when I try test don't work.
I don't know if I doing it right or I have to do this in a different way.

The code that you sent to me don't have a method main, so I create one.

I did the code like this:

public static void main (String args[]) throws SOAPException{

Token token = new Token();
token.getToken();

System.out.println("The token is: "+ token);
}

But occurs the follow error:

javax.xml.soap.SOAPException: no protocol: org.apache.axis2.util.URL@225c25d9O erro foi: javax.xml.soap.SOAPException: no protocol: org.apache.axis2.util.URL@225c25d9
The token is: br.com.zimbraws.client.Token@5f92e49f

at org.apache.axis2.saaj.SOAPConnectionImpl.call(SOAPConnectionImpl.java:128)
at br.com.zimbraws.client.Token.getToken(Token.java:64)
at br.com.zimbraws.client.ZimbraWS.main(ZimbraWS.java:13)

I think is an error of URL, and I'm using like this:

URL url = new URL("https://postmail.myservice.com.br/servi ... rvice.wsdl");

Sorry for ask so much, but I search for some explanation about java.xml but I don't found much information about that.

Can you help me? posting some documents or link that explain how I use this of the right way, or help me with some more detail of how I make the integration with java and Zimbra.

Thank you again.

Best Regards
Rafael.
G71
Posts: 23
Joined: Fri Aug 19, 2016 10:01 am

Re: How to generate the authtoken with Java?

Post by G71 »

Rafael wrote:Hi G71,

Thank you for the help again.

I never used the axis2 before, is the first time, and I don't know well how it work.

But I Use the code that you sent to me, I write line to line, to understand better what I'm doing, but when I try test don't work.
I don't know if I doing it right or I have to do this in a different way.

The code that you sent to me don't have a method main, so I create one.

I did the code like this:

public static void main (String args[]) throws SOAPException{

Token token = new Token();
token.getToken();

System.out.println("The token is: "+ token);
}

But occurs the follow error:

javax.xml.soap.SOAPException: no protocol: org.apache.axis2.util.URL@225c25d9O erro foi: javax.xml.soap.SOAPException: no protocol: org.apache.axis2.util.URL@225c25d9
The token is: br.com.zimbraws.client.Token@5f92e49f

at org.apache.axis2.saaj.SOAPConnectionImpl.call(SOAPConnectionImpl.java:128)
at br.com.zimbraws.client.Token.getToken(Token.java:64)
at br.com.zimbraws.client.ZimbraWS.main(ZimbraWS.java:13)

I think is an error of URL, and I'm using like this:

URL url = new URL("https://postmail.myservice.com.br/servi ... rvice.wsdl");

Sorry for ask so much, but I search for some explanation about java.xml but I don't found much information about that.

Can you help me? posting some documents or link that explain how I use this of the right way, or help me with some more detail of how I make the integration with java and Zimbra.

Thank you again.

Best Regards
Rafael.
Try with

Code: Select all

https://postmail.myservice.com.br:[yourserverPort]/service/soap/
instead of

Code: Select all

https://postmail.myservice.com.br/service/wsdl/ZimbraService.wsdl
Post Reply