Saturday, February 7, 2015

How to activate the Campaign in Eloqua through java

How to activate the Campaign in Eloqua through java

This post explains how to activate the Eloqua campaign through java.

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 activate the campaign

Change the Eloqua credential accordingly.

public class CampaignHelper
{
   private RestClient client;

   public CampaignHelper(String site, String user, String password, String baseUrl)
   {
      client = new RestClient(site + "\\" + user, password, baseUrl);
   }

   public void activateCampaign(String campaignId) throws Exception{

       try {
     String response = null;
     response = client.execute("/assets/campaign/"+campaignId,"GET",null);
     if(null!=response){      
        response = client.execute("/assets/campaign/active/"+campaignId,"POST", response);  
        if(null == response){
     throw new Exception("Not able to activate the campaign");
  }
       }else
       {
    throw new Exception("Not able to get the campaign");
       }

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

     public static void main(String[] args) {
 CampaignHelper helper=new CampaignHelper("companyname", "username", "password", "https://secure.p03.eloqua.com/api/rest/2.0");
 try {
  helper.activateCampaign("143");
  } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  }

      }
}

1 comment:

  1. Hi Albin,

    Thank you for the post, is there any way that we can find the campaigns that are active?

    ReplyDelete