How to get the Subscriptions of a Contact through through Java API in Eloqua
This post explains how to create the contact in Eloqua through java.Create the required model classes:
public class SubscriptionResponse{
public List<Subscription> elements;
}
public class Subscription
{
public EmailGroup emailGroup;
public String isSubscribed;
}
public class EmailGroup
{
public String type;
public String id;
public String depth;
public String description;
public String name;
public String updatedAt;
public String updatedBy;
}
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");
System.out.println(authToken);
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 get the contact subscriptions:
Change the Eloqua credential accordingly.
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import models.SubscriptionResponse;
import .models.Subscription;
public class SubsceiptionHelper
{
private RestClient client;
public SubsceiptionHelper(String site, String user, String password, String baseUrl)
{
client = new RestClient(site + "\\" + user, password, baseUrl);
}
public Map<String,String> getSubscriptionList(String contactid) throws Exception{
Map<String,String> emailIdMap = new HashMap<String, String>();
try {
String response = null;
Gson gson = new GsonBuilder().serializeNulls().create();
response = client.execute("/data/contact/"+contactid+"/email/groups/subscription","GET",null);
System.out.println(response);
if(null!=response){
SubscriptionResponse searchResponse = gson.fromJson(response, SubscriptionResponse.class);
List<Subscription> subscriptionList = searchResponse.elements;
for(Subscription subscription :subscriptionList){
if(null !=subscription.isSubscribed){
if((subscription.isSubscribed.equals("true"))){
emailIdMap.put(subscription.emailGroup.id, subscription.emailGroup.name);
}
}
}
}
} catch (Exception e) {
throw e;
}
return emailIdMap;
}
public static void main(String[] args) {
SubsceiptionHelper helper=new SubsceiptionHelper("companyname", "username", "password", "https://secure.p03.eloqua.com/api/rest/1.0");
try {
System.out.println(helper.getSubscriptionList("1"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
great post.
ReplyDeletedo you know of anyway you can get all contacts that are subscribed or unsubscribed to the email groups rather than just querying one?