Saturday, February 14, 2015

How to create the contact in Eloqua through java

How to create the contact in Eloqua through java

This post explains how to create the contact in Eloqua through java.

Create the model class for Contact:

public class Contact
{
 public String accountName;
 public String address1;
 public String address2;
 public String address3;
 public String businessPhone;
 public String city;
 public String country;
 public String firstName;
 public String emailAddress;
 public String id;
 public boolean isBounceback;
 public boolean isSubscribed;
 public String lastName;
 public String salesPerson;
 public String title;
 public String type;
 public String name;

}

RestClient class to connect to Eloqua:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class RestClient
{
 private String authToken;
 private String baseUrl;

 public RestClient(String user, String password, String url)
 {
  baseUrl = url;
  String authString = user + ":" + password;
  authToken = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(authString.getBytes());        }

 public String execute(String uri, String method, String body)  throws Exception
 {
  String response ="";
  try
  {
   URL url = new URL(baseUrl + uri);
   HttpURLConnection conn = (HttpURLConnection) url.openConnection();

   conn.setInstanceFollowRedirects(false);
   conn.setRequestMethod(method.toString());
   conn.setRequestProperty("Content-Type", "application/json");
   conn.setRequestProperty("Accept", "application/json");
         conn.setRequestProperty("Authorization", authToken);    
       
         if (method == "POST" || method == "PUT")
         {
          if(null != body){
    conn.setDoOutput(true);
          final OutputStream os = conn.getOutputStream();
                os.write(body.getBytes());
                os.flush();
                os.close();
          }
         }
       
         InputStream is = conn.getInputStream();
         BufferedReader rd = new BufferedReader(new InputStreamReader( is));

          String line;
         while ((line = rd.readLine()) != null)
         {
             response += line;
         }      
         rd.close();
         conn.disconnect();
  }
  catch (Exception e)
  {
   throw e;
  }
        return response;
 }
}



The Helper class to create the contact

Change the Eloqua credential accordingly.

import com.google.gson.Gson;

public class ContactHelper {

private RestClient client;

 public ContactHelper(String site, String user, String password, String baseUrl)
 {
  client = new RestClient(site + "\\" + user, password, baseUrl);
 }
 public void createContact(Contact contact) throws Exception{
   try {
  String response = null;
  Gson gson = new Gson();
  String contactRequest = gson.toJson(contact);
   response = client.execute("/data/contact","POST",contactRequest);
   System.out.println(response);
   if(null==response){      
       throw new Exception("Not able to get the campaign");
   }

   } catch (Exception e) {
    throw e;
   }
 }

 public static void main(String[] args) {
  ContactHelper helper=new ContactHelper("companyname", "username", "password", "https://secure.p03.eloqua.com/api/rest/2.0");
  try {
     Contact contact = new Contact();
     contact.firstName = "Albin";
     contact.lastName = "Issac";
     contact.isSubscribed = true;
     contact.type = "Contact";
     contact.emailAddress = "albinsharp@gmail.com";
   
     helper.createContact(contact);
 
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}



2 comments:

  1. Albin,
    Great document! It helped a lot. I used similar code for updating a contact. However, I got a 400 error. I think my json string is not correct. It was {"type": "Contact","id": "1104497","isSubscribed": "false"}. Can you help me to find what is wrong with my json string? Thank you.

    Brian

    ReplyDelete
    Replies
    1. Hi Brain

      emailAddress is mandatory to update the contact.

      Use the below json string.

      {"type": "Contact","id": "1104497","emailAddress":"albin@gmail.com","isSubscribed": "false"}

      Regards
      Albin

      Delete