Problem in running the SyncLead sample code in Java

Anonymous
Not applicable

Problem in running the SyncLead sample code in Java

I tried to run the sample code in Java for SyncLead and got errors:

javax.xml.bind.MarshalException
 - with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "com.marketo.mktows.SuccessSyncLead" as an element because it is missing an @XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(Unknown Source)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
at pkg.client.TestMarketoSyncLead.main(TestMarketoSyncLead.java:83)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "com.marketo.mktows.SuccessSyncLead" as an element because it is missing an @XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(Unknown Source)
... 4 more

This also happened in other samples as well. Here is the sample code:

 
package pkg.client;
 
import com.marketo.mktows.*;
import java.net.URL;
import javax.xml.namespace.QName;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Hex;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
 
 
public class TestMarketoSyncLead {
 
    public static void main(String[] args) {
        System.out.println("Executing syncLead");
        try {
            URL marketoSoapEndPoint = new URL("https://100-AEK-913.mktoapi.com/soap/mktows/2_1" + "?WSDL");
            String marketoUserId = "demo17_1_809934544BFABAE58E5D27";
            String marketoSecretKey = "27272727aa";
             
            QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
            MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
            MktowsPort port = service.getMktowsApiSoapPort();
             
            // Create Signature
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
            String text = df.format(new Date());
            String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);           
            String encryptString = requestTimestamp + marketoUserId ;
             
            SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init(secretKey);
            byte[] rawHmac = mac.doFinal(encryptString.getBytes());
            char[] hexChars = Hex.encodeHex(rawHmac);
            String signature = new String(hexChars); 
             
            // Set Authentication Header
            AuthenticationHeaderInfo header = new AuthenticationHeaderInfo();
            header.setMktowsUserId(marketoUserId);
            header.setRequestTimestamp(requestTimestamp);
            header.setRequestSignature(signature);
             
            // Create Request
            ParamsSyncLead request = new ParamsSyncLead();
            LeadRecord key = new LeadRecord();
             
            ObjectFactory objectFactory = new ObjectFactory();
            JAXBElement<String> email = objectFactory.createLeadRecordEmail("george@jungle.com");
            key.setEmail(email);
            request.setLeadRecord(key);
             
            Attribute attr1 = new Attribute();
            attr1.setAttrName("FirstName");
            attr1.setAttrValue("George2");
             
            Attribute attr2 = new Attribute();
            attr2.setAttrName("LastName");
            attr2.setAttrValue("of the Jungle");
             
            ArrayOfAttribute aoa = new ArrayOfAttribute();
            aoa.getAttribute().add(attr1);
            aoa.getAttribute().add(attr2);
             
            QName qname = new QName("http://www.marketo.com/mktows/", "leadAttributeList");
            JAXBElement<ArrayOfAttribute> attrList = new JAXBElement(qname, ArrayOfAttribute.class, aoa);         
            key.setLeadAttributeList(attrList);
             
            MktowsContextHeaderInfo headerContext = new MktowsContextHeaderInfo();
            headerContext.setTargetWorkspace("default");
             
            SuccessSyncLead result = port.syncLead(request, header, headerContext);
 
            JAXBContext context = JAXBContext.newInstance(SuccessSyncLead.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(result, System.out);
             
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }
}


Noted that I had to change some classes for it to compile:
AuthenticationHeader to AuthenticationHeaderInfo
MktowsContextHeader to MktowsContextHeaderInfo
getAttributes() to getAttribute()

Thanks for your time!

Tags (1)
7 REPLIES 7
Anonymous
Not applicable

Re: Problem in running the SyncLead sample code in Java

Hi Ken,

Did you use the syncLead sample code from the Marketo developers site?

I would suggest investigating the cause of the initial compilation errors rather than try to workaround it by modifying the class references. Once you've reverted this, if you're still stuck, paste your code & compilation errors and hopefully somebody will be able to help.

(My first guess is that you need to change the marketoSoapEndPoint to be your unique instance end point, as mentioned in the SOAP documentation.)
Anonymous
Not applicable

Re: Problem in running the SyncLead sample code in Java

Yes, I used  the sample code from the Marketo developers site. The reason for the workaround problem was that those methods don't exist in the wsdl file that I imported using the wsimport command. For the endpoint, I tried that too, but it still fails.
Anonymous
Not applicable

Re: Problem in running the SyncLead sample code in Java

This may be obvious, but I forgot to mention that you'll also need to modify the marketoUserId marketoSecretKey variables too as per the values on your Admin page.

Regarding your errors, perhaps this stackoverflow thread will point you in the right direction. 

Hope this helps!
Anonymous
Not applicable

Re: Problem in running the SyncLead sample code in Java

I tried that and got different error. Here the output:

Executing syncLead
javax.xml.ws.soap.SOAPFaultException: 20126 - Workspace not found
at com.sun.xml.internal.ws.fault.SOAP11Fault.getProtocolException(Unknown Source)
at com.sun.xml.internal.ws.fault.SOAPFaultBuilder.createException(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SyncMethodHandler.invoke(Unknown Source)
at com.sun.xml.internal.ws.client.sei.SEIStub.invoke(Unknown Source)
at com.sun.proxy.$Proxy34.syncLead(Unknown Source)
at pkg.client.TestMarketoSyncLead.main(TestMarketoSyncLead.java:80)

The error points to this line in the code:

SuccessSyncLead result = port.syncLead(request, header, headerContext);

Do you have any sample code(s) that allow me to add a lead (with email info) to our campaign? Thanks!
Anonymous
Not applicable

Re: Problem in running the SyncLead sample code in Java

Looks like the workspace name you are supplying does not match the name in the subscription
Anonymous
Not applicable

Re: Problem in running the SyncLead sample code in Java

How do you fix it? I'm new to both Java and Marketo, so please be more specific. Thanks!
Anonymous
Not applicable

Re: Problem in running the SyncLead sample code in Java

I forgot to mention that if I used the credentials provided by Marketo in the sample code (Marketo Host, User Id, Secret Key) then it worked fine except that I don't know where it went. I couldn't use the login id/password provided to me by Marketo to verify it. However, when I used different set of credentials (HUS), provided by Marketo, to run the sample, I got the mentioned above error (20126 - Workspace not found).