Integrating an email contact for an existing Constant Contact customer

Regular Participant

Integrating an email contact for an existing Constant Contact customer

I'm trying to integrate the mashery api for an existing Constant Contact customer. I have their CC account information and have access to their console. I also created a mashery developer account (for myself, with my own personal information) and obtained my own apiKey and accessToken which the Java SDK leverages when a ConstantContact(apiKey, accessToken) object is created.

 

My two questions are:

1.) How do I link up my devloper account (apiKey, accessToken) to my customers Constant Contact account so that email addresses that I attempt to add to their account are added to their account and proper email list/campaign?

 

2.) How do I indicate which email campaign I want to add the provided email address to? 

 

Apologies for the beginner questions, I've searched around and don't see how I can link this all up. All I'm trying to do is take an email address (entered on a web form), and call the mashery api to add it to my clients existing Constant Contact account.

6 REPLIES 6
Regular Participant

I resolved my first question by 'granting access' through another browser. For some reason the Single-SignOn implementation allowed me to grant access to myself, even though I don't have a Constant Contact account. When I went through the steps described here (http://developer.constantcontact.com/home/api-keys.html), under Request an API Key, step 3, bullet point 2, I was able to login with my clients Constant Contact credentials and granted access to their account. I then received an access token that allowed me to use the api to query their account. Perfect.

 

Now that I'm using the java sdk, the ConstantContact.addContact() method is throwing an exception indicating this:

 

We expected a json array but we received an a json object for one of the fields in the payload.

 

I'm printing out the Contact object and here are the values:

 

com.constantcontact.components.contacts.Contact@4114487d[id=<null>,status=<null>,fax=<null>,addresses=[],notes=[],confirmed=true,lists=[ContactList [id=1, status=ACTIVE, name=General Interest, contactCount=110]],source=<null>,emailAddresses=[EmailAddress [status=null, confirmStatus=null, optInSource=null, optInDate=null, optOutDate=null, emailAddress=billy@xxxxxxx.com]],prefixName=<null>,firstName=Hope Foods,middleName=<null>,lastName=Web Site Newsletter Request,jobTitle=<null>,companyName=<null>,customFields=[],sourceDetails=<null>,workPhone=<null>,homePhone=<null>,cellPhone=<null>,createdDate=2014-02-16T22:37:29.944+0000,modifiedDate=<null>]

 

What am I doing wrong and what array are they talking about that should be json? I'm using the java sdk.

Hi Robert,

 

I'm not seeing the issue with your values offhand, but you can look at the contact add/update example in the java SDK to see how the contact object & arrays inside it are set up.  Most of the setup is done in the ContactDetailsActivity file.

 

Best Regards,

Shannon W.

Partner API Support Engineer

Thank you for the reply Shannon.

 

Coincidently that's exactly the place I looked to verify what I was doing. I noticed these today from the ContactDetailsActivity class in the sdk example.

 

		contact = new Contact();
		contact.setAddresses(new ArrayList<Address>());
		contact.setEmailAddresses(new ArrayList<EmailAddress>());
		contact.setCustomFields(new ArrayList<CustomField>());
		contact.setLists(new ArrayList<ContactList>());

 So I added these initializers to my code. Here's my code which is fairly straightforward.

 

	public boolean register(final String email) {
		boolean success;
		try {

			// initialize contact object
			final Contact contact = new Contact();
			contact.setAddresses(new ArrayList<Address>());
			contact.setCustomFields(new ArrayList<CustomField>());
			contact.setConfirmed(true);
			contact.setCreatedDate(sdf.format(new Date()));

			final List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
			emailAddresses.add(new EmailAddress(email));

			contact.setEmailAddresses(emailAddresses);
			contact.setLists(getContactListsForNewContact());

			System.out.println(ReflectionToStringBuilder.toString(contact));

			cc.addContact(contact, false);

			success = true;
		} catch (ConstantContactServiceException e) {
			System.out.println( e.getErrorInfo().get(0).getErrorMessage() );
			success = false;
		}

		return success;
	}

 

What else can I do to troubleshoot and possibly get back a more descriptive error message from the api, or web service?

I'm still receiving the following error from the Java SDK:

 

We expected a json array but we received an a json object for one of the fields in the payload.

 

I added some additional logging and still don't see why the API is rejecting my addContact request:

 

{
  "id" : null,
  "status" : null,
  "fax" : null,
  "addresses" : [ ],
  "notes" : [ ],
  "confirmed" : true,
  "lists" : [ {
    "id" : "1",
    "status" : "ACTIVE",
    "name" : "General Interest",
    "contactCount" : 112,
    "createdDate" : "2012-08-17T20:50:26.000Z"
  } ],
  "source" : null,
  "emailAddresses" : [ {
    "id" : null,
    "status" : null,
    "confirmStatus" : null,
    "optInSource" : null,
    "optInDate" : null,
    "optOutDate" : null,
    "emailAddress" : "billy@nichez.com"
  } ],
  "prefixName" : null,
  "firstName" : null,
  "middleName" : null,
  "lastName" : null,
  "jobTitle" : null,
  "companyName" : null,
  "customFields" : [ ],
  "sourceDetails" : null,
  "workPhone" : null,
  "homePhone" : null,
  "cellPhone" : null,
  "createdDate" : "2014-02-18T22:17:05.765+0000",
  "modifiedDate" : null
}

 Is what I'm sending as data incorrect?

 

I believe I've resolved the issues here, which were mostly due to the bogus error message that I was getting back from the API. Not sure why I was getting back the error message I was to be honest, but when I created a unit test for the constant-contact-sdk (ConstantContact class in particular), I was able to uncover what was going on. 

 

My testing steps went like this.

 

  1. I added a new contact to my clients General Interest list. 
  2. Logged into the Constant Contact console and verifed that the email address was indeed there.
  3. I then would try to add the email address again. I would get that bogus 'payload array' message indicated in one of my previous threads, which didn't make any sense.
  4. I then deleted the email address from the Constant Contact admin console. Verifed it was no longer there.
  5. Then I'd run the test again, and it would continue to fail. The ConstantContactTest I wrote helped me identify that I was actually getting back a 'xxxx email address already exists' message. This confused me even more because I verifed that the email address did not exist through the admin console.
  6. After adding an additional test method, which attempted to retrieve a contact by email address, it indeed found the contact previously created. Although, the status of this contact was REMOVED. 

So there must be a caching period or something that leaves this Contact in the system, in a removed/deleted state.

 

What I'm doing now in my code is attempting to fetch the contact by the email address provided, rather than just blindly adding the contact. It still puts things in a weird state but it's working.

 

The final code looks like this:

 

	public boolean register(final String email) {
		boolean success;
		try {

			// first check to see if this email is already signed up.
			List<Contact> contacts = cc.getContactByEmail(email).getResults();
			if (!contacts.isEmpty()) return true; // short circuit here if this email address is found.

			// initialize contact object
			final Contact contact = new Contact();
			contact.setConfirmed(true);
			contact.setCreatedDate(sdf.format(new Date()));
			contact.setAddresses(new ArrayList<Address>());
			contact.setCustomFields(new ArrayList<CustomField>());
			contact.setLists(getContactListsForNewContact());
			contact.setNotes(new ArrayList<Note>());

			final List<EmailAddress> emailAddresses = new ArrayList<EmailAddress>();
			emailAddresses.add(new EmailAddress(email));

			contact.setEmailAddresses(emailAddresses);

			cc.addContact(contact);

			success = true;
		} catch (ConstantContactServiceException e) {
			List<CUrlRequestError> errors = e.getErrorInfo();

			for (CUrlRequestError error : errors) {
				System.out.println(error.toString());
			}

			success = false;
		} catch (Exception e) {
			e.printStackTrace();
			success = false;
		}

		return success;
	}

 

 

Sorry for the confusion with the deleted contact.  When you delete a Contact in our UI, it is not a permanent delete.  The Contact continues to exist behind the scenes and if you search for the email address, or use the GetContacts method to get all Contacts through the API, you'll notice that the Contact is indeed still there and has all of the original information.  The only difference is that the Contact will be in Removed state. 

 

Certainly understand why this could be confusing since the UI no longer shows these Removed Contacts.  They are indeed still there though.  Will forward that feedback on to our development team.

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

View API documentation, code samples, get your API key.

Visit Page