Accessing Commission Junction’s API with Java

I couldn’t find a good example of how to connect to the CJ API with Java, so here is stripped down version of how I did it:

import java.net.*;
import java.io.*;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
 
import org.w3c.dom.Document;
 
...
 
public static Document getCommissionDetail(String startDate, String endDate){
    String response = "";
    String devKey = "YOUR-DEV-KEY";
    Document doc = null;
    String urlPath = "https://commission-detail.api.cj.com/v3/commissions?date-type=event&start-date=" + startDate + "&end-date=" + endDate;
 
      try {
        URL u = new URL(url);
        URLConnection uc = u.openConnection();
        HttpURLConnection connection = (HttpURLConnection) uc;
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Authorization", devKey) ;
        connection.setRequestProperty("Content-Type","text/xml");
        connection.setDoInput(true);
 
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(connection.getInputStream());
        doc.getDocumentElement().normalize();
 
      }
      catch (IOException e) {
        //Something went wrong with the request
      }
      catch (Exception e) {
        //Something went wrong, very wrong
      }
    return doc;
}

Leave a comment if you find it useful or use it in a project!