2 Replies Latest reply on Jan 27, 2014 4:37 PM by eebe3dc7c9d1d31cd4793c8814dcea8a601352bd

    GetLeadActivity pagination in not working

      I can't get GetLeadActivity pagination working (when set is > 100). See my sample code below.

      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 GetLeadActivity_Threaded {

          public static void main(String[] args) {
              System.out.println("Executing Get Lead Activity");
              try {

                  URL marketoSoapEndPoint = new URL("https://176-JQK-087.mktoapi.com/soap/mktows/2_2" + "?WSDL");
                  String marketoUserId = "axialmarket11_7615930150E701E2A69A51";
                  String marketoSecretKey = "kirill8288";

                  QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
                  MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
                  MktowsPort port = service.getMktowsApiSoapPort();

                  // Create Request
                  ParamsGetLeadActivity request = new ParamsGetLeadActivity();
                  LeadKey key = new LeadKey();
                  key.setKeyType(LeadKeyRef.IDNUM);
                  key.setKeyValue("327");
                  request.setLeadKey(key);

                  // 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
                  AuthenticationHeader header = new AuthenticationHeader();
                  header.setMktowsUserId(marketoUserId);
                  header.setRequestTimestamp(requestTimestamp);
                  header.setRequestSignature(signature);

                  SuccessGetLeadActivity result = null;

                  do {

                      result = port.getLeadActivity(request, header);

                      System.err.println("returned: " + result.getLeadActivityList().getReturnCount());
                      System.err.println("remaining: " + result.getLeadActivityList().getRemainingCount());
                      System.err.println("new start position offset: " + result.getLeadActivityList().getNewStartPosition().getOffset().getValue());

                      // JAXBContext context = JAXBContext.newInstance(SuccessGetLeadActivity.class);
                      // Marshaller m = context.createMarshaller();
                      // m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
                      // m.marshal(result, System.out);

                      // loop starts here |
                      // ---------------- V

                      // Recreate Signature
                      df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
                      text = df.format(new Date());
                      requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
                      encryptString = requestTimestamp + marketoUserId ;
                      secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
                      mac = Mac.getInstance("HmacSHA1");
                      mac.init(secretKey);
                      rawHmac = mac.doFinal(encryptString.getBytes());
                      hexChars = Hex.encodeHex(rawHmac);
                      signature = new String(hexChars);

                      // Reset Authentication Header
                      header = new AuthenticationHeader();
                      header.setMktowsUserId(marketoUserId);
                      header.setRequestTimestamp(requestTimestamp);
                      header.setRequestSignature(signature);

                      // Advance to the next position
                      // THIS IS NOT WORKING

                      ObjectFactory objectFactory = new ObjectFactory();
                      StreamPosition streamPosition = objectFactory.createStreamPosition();
                      JAXBElement<String> offset = objectFactory.createStreamPositionOffset(result.getLeadActivityList().getNewStartPosition().getOffset().getValue());
                      streamPosition.setOffset(offset);

                      request.setStartPosition(new JAXBElement<StreamPosition>(new QName("StreamPosition"), StreamPosition.class, streamPosition));

                  } while (result.getLeadActivityList().getRemainingCount() > 0);

              }
              catch(Exception e) {
                  e.printStackTrace();
              }
          }
      }