Got a 'How do I' question? Join our live 'Ask a Trainer' session happening now for instant help and pro tips!

I am able to read data using access_token but not able to post data to constant contact through api

KitoodleCreators
Rookie
0 Votes

Please see below code. one is for read and second is for posting data 


<?php
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.cc.email/v3/contact_lists?include_count=true&status=active&include_membership_count=all',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'Accept: */*',
'Content-Type: application/json',
'Authorization: Bearer '.$access_token
),
));

$response = curl_exec($curl);

curl_close($curl);
echo '<pre>'; print_r(json_decode($response));

exit;

?>

<?php

$url = 'https://api.cc.email/v3/contacts';
$postdata = '{
"email_address": {
"address": "sibinfotech101@gmail.com",
"permission_to_send": "implicit"
},
"create_source": "Account",
"custom_fields": [
{
"custom_field_id": "0123895a-35cd-11ee-a3a3-fa163eb65cb7",
"value": "Tushar"
},
{
"custom_field_id": "485b7d4a-35dd-11ee-a3a3-fa163eb65cb7",
"value": "9099887766"
},
{
"custom_field_id": "0863b528-35cd-11ee-bcfd-fa163eb65cb7",
"value": "Test Subject"
},
{
"custom_field_id": "0d7f2f92-35cd-11ee-bcfd-fa163eb65cb7",
"value": "Test Message"
}
]
}';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Cache-Control' => 'no-cache',
'Authorization' => 'Bearer '.$access_token,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);

exit;

?>

3 REPLIES 3
John__B
Moderator
0 Votes

Hello KitoodleCreators,

 

Thank you for reaching out to Constant Contact API Developer Support. My team is here to assist outside software developers with questions about building into Constant Contact's API.

 

It looks like we've already followed up with you directly via email, but I wanted to close the loop here as well. If you haven't received our reply, please let us know via email at webservices@constantcontact.com, and reference case number 31447618.

 

Please have a look and let us know if you have any other questions!

 

Regards,


John B.
API Support Specialist
Did I answer your question? If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
KitoodleCreators
Rookie
0 Votes

Dear sir, 

 

i have used the same request body as shown in below link referred by your team member
https://developer.constantcontact.com/api_reference/index.html#!/Contacts/createContact

 

but still getting this error
{"error_key":"unauthorized","error_message":"Unauthorized"}

 

here is my code -

$url = 'https://api.cc.email/v3/contacts';
$postdata = '{
"email_address": {
"address": "sibinfotech101@gmail.com",
"permission_to_send": "implicit"
},
"create_source": "Contact",
"custom_fields": [
{
"custom_field_id": "0123895a-35cd-11ee-a3a3-fa163eb65cb7",
"value": "Tushar"
},
{
"custom_field_id": "485b7d4a-35dd-11ee-a3a3-fa163eb65cb7",
"value": "9099887766"
},
{
"custom_field_id": "0863b528-35cd-11ee-bcfd-fa163eb65cb7",
"value": "Test Subject"
},
{
"custom_field_id": "0d7f2f92-35cd-11ee-bcfd-fa163eb65cb7",
"value": "Test Message"
}
]
}';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Cache-Control' => 'no-cache',
'Authorization' => 'Bearer '.$access_token,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
));
$result = curl_exec($ch);
curl_close($ch);
print_r ($result);

exit;

 

please check and advise any corrections to be made in this request. I am still able to get the data using get request but not able to post

 

Thanks

 

John__B
Moderator
0 Votes

Hello KitoodleCreators,

 

Thank you for your reply. The “Unauthorized” error response that you encountered would typically indicate that the access_token used in the authorization header of your request is expired or invalid. If you continue to encounter this issue, I would recommend generating a new access_token and ensuring that your application is properly saving and adding the new token value to the authorization header of subsequent requests. 

 

Upon reviewing our logs for the most recent requests made for your account using the code you provided, it appears that authorization was successful but the requests failed with 409 error responses due to the contact already existing in your account. 

 

Once a contact (email address) has been added into the system, even if it is deleted, the contact entry and details still remain on the back end of the system for reporting and compliance purposes. When using the API, there are a couple of different endpoint options to bypass the 409 response. Typically this error occurs when making a POST call to the /contacts endpoint, which can only add new contacts whose email addresses have not previously been added to the account. Essentially, deleting a contact never really deletes them. It just strips their list memberships, moves them into a “deleted” status, and hides them from being visible in the account.

 

Frequently Asked Questions for Contacts

https://www.constantcontact.com/legal/about-constant-contact-faqs

 

Additionally, when working with unsubscribed contacts via the API, you'll need to ensure that resubscribing the contact is permission-based (they have to confirm that they want to be resubscribed and resume receiving emails) to meet all applicable laws and regulations, as well as our terms of service.

 

Constant Contact's email permission policy

https://knowledgebase.constantcontact.com/articles/KnowledgeBase/5305-constant-contact-s-email-permi...

 

That being said, in this case you are going to need to use the GET /contacts endpoint using the value "all" ("status":"all"). Using the "status" query parameter with the value "all" returns all contacts including existing, deleted, and unsubscribed contacts, which is needed with your use case.

 

Next, you'll want to use all of the fields for the "include" parameter, because if the contact exists or previously existed within the account, we'll have to use PUT (rather than POST), which overwrites any existing data (more on that further down)

 

Here's the flow you'll want to use:

 

Step 1: Check to see if the contact exists in the account using the email query parameter to search for a contact using a specific email address.

 

For the GET /contacts call, this is how we'll want to format the request URL to make sure we're checking for deleted or unsubscribed contacts, rather than just active ones:

 

GET

https://api.cc.email/v3/contacts?email=dlang@example.com&status=all&include=custom_fields,list_membe...

 

If you get a blank response body at this point, then you can use your POST call to create a new contact.

{

"contacts": []

}

 

However, if a contact entry is returned in the response body, you'll need to update that entry using PUT /contacts instead:

{

"contacts": [

{

"contact_id": "1618ae62-4752-11e9-9c8a-fa163e6b01c1",

"email_address": {

"address": "dlang@example.com",

"permission_to_send": "implicit",

[...]

 

If the contact comes back as already existing in the account. You need to append the returned data with the desired list memberships and send that back with PUT to update the contact.

 

When Updating a contact, we need to append the new information to the old information and send all of it back in the request, because when you update a contact using a PUT, all properties are updated, overwriting all existing property values. Any properties left blank or not included in the PUT will be overwritten with a null value.

 

So, from that response body, the "contact_id" is going to go in our PUT /contacts URL, and then you'll take the rest of the data from the response to build your request body like so:

 

PUT

https://api.cc.email/v3/contacts/1618ae62-4752-11e9-9c8a-fa163e6b01c1

{

"email_address": {

"address": "dlang@example.com",

"permission_to_send": "implicit",

[...]

 

Important things to note when updating the contact:

 

When updating {"permission_to_send": "unsubscribed"} to {"permission_to_send": "implicit"}, you need to also change the update source to Contact, because the permission can't be changed by the account, like so: {"update_source": "Contact"}.

 

Additionally, if the contact already exists as an active contact in the account (not deleted or unsubscribed), you should still do a PUT call in case they are not already on the desired list. You'll want to append to, rather than replace the existing lists (and other data) so that they are added to the correct list without being removed from other lists they are on:

"list_memberships": [

"07936f78-662a-11eb-af0a-fa163e56c9b0", "04fe9a-a579-43c5-bb1a-58ed29bf0a6a, {add desired list here and send back all three}"

]

 

Please have a look and let us know if you have any other questions!

 

Regards,


John B.
API Support Specialist
Did I answer your question? If so, please mark my post as an "Accepted Solution" by clicking the Accept as Solution button in the bottom right hand corner of this post.
Resources
Developer Portal

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

Visit Page

Announcements

API Updates

Join our list to be notified of new features and updates to our V3 API.

Sign Up