The Community is hosting an End of Summer sweepstakes! Participants must complete tasks to earn tickets that will enter them with a chance to win a free year of Constant Contact and other great prizes!*
*No Purchase Necessary. For Official Rules, visit here. Constant Contact’s End of Summer 2020 Sweepstakes ends on October, 20, 2020 at 11:50 PM EST.

An Authentication object was not found in the SecurityContext

Highlighted
Occasional Participant

An Authentication object was not found in the SecurityContext

Respecte sir,


sir, i am using api code in php. i am getting 401 error. please find my code below.


we have used in asp.net, but while in php we get error.


kindly provide solutions


$email = info@testing.com;



/////////// REGISTER EMAIL WITH CONSTANT CONTACT ///////////////////



$UN = "username";

$PW = "password";

$Key = "c07a7f0d-c240-417e-971b-cdfc4d7c18c7";



$entry = '<entry xmlns="http://www.w3.org/2005/Atom">

<title type="text"> </title>

<updated>' . date('c') . '</updated>

<author></author>

<id>data:,none</id>

<summary type="text">Contact</summary>

<content type="application/vnd.ctct+xml">

<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">

<EmailAddress>' . $email . '</EmailAddress>

<OptInSource>ACTION_BY_COSTOMER</OptInSource>

<ContactLists>

<ContactList id="http://api.constantcontact.com/ws/customers/'.$UN.'/lists/1"/>

</ContactLists>

</Contact>

</content>

</entry>';


$request ="http://api.constantcontact.com/ws/customers/".$UN."/contacts";

$session = curl_init($request);



// Set up digest authentication

$userNamePassword = $Key . '%' . $UN . ':' . $PW ;



// Set cURL options

curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);

curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);

curl_setopt($session, CURLOPT_POST, 1);

curl_setopt($session, CURLOPT_POSTFIELDS , $entry);

curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));

curl_setopt($session, CURLOPT_HEADER, false); // Do not return headers

curl_setopt($session, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response



// Execute cURL session and close it

 

$response = curl_exec($session);

$httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);

echo "Code=>".$httpcode;

curl_close($session);


 


let us know if we have to do extra setting in constant contact.


thanks


 

1 REPLY 1
Highlighted
Employee

Re: An Authentication object was not found in the SecurityContex

There were a few modifications that I needed to make to your above code to get this to successfully create this contact in your account.


1. $request must be changed to an https url


2. <OptInSource> must be "ACTION_BY_CUSTOMER" rather than "ACTION_BY_COSTOMER"


3. curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0); must be added as a curl option.


4. curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); should be changed to curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); as Constant Contact supports basic authentication but not digest.


The code below should work for you, but if you have any questions or concerns with this please let us know. Thanks.


<?php

$email = 'info@testing.com';



/////////// REGISTER EMAIL WITH CONSTANT CONTACT ///////////////////



$UN = "USERNAME";

$PW = "PASSWORD";

$Key = "APIKEY";



$entry = '<entry xmlns="http://www.w3.org/2005/Atom">

<title type="text"> </title>

<updated>'
. date('c') . '</updated>

<author></author>

<id>data:,none</id>

<summary type="text">Contact</summary>

<content type="application/vnd.ctct+xml">

<Contact xmlns="http://ws.constantcontact.com/ns/1.0/">

<EmailAddress>'
. $email . '</EmailAddress>

<OptInSource>ACTION_BY_CUSTOMER</OptInSource>

<ContactLists>

<ContactList id=http://api.constantcontact.com/ws/customers/'/>

</ContactLists>

</Contact>

</content>

</entry>'
.$UN.'/lists/1;

$request ="https://api.constantcontact.com/ws/customers/".$UN."/contacts";

$session = curl_init($request);



// Set up digest authentication

$userNamePassword = $Key . '%' . $UN . ':' . $PW ;



// Set cURL options

curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt($session, CURLOPT_USERPWD, $userNamePassword);

curl_setopt($session, CURLOPT_POST, 1);

curl_setopt($session, CURLOPT_SSL_VERIFYPEER, 0);

curl_setopt($session, CURLOPT_POSTFIELDS , $entry);

curl_setopt($session, CURLOPT_HTTPHEADER, Array("Content-Type:application/atom+xml"));

curl_setopt($session, CURLOPT_HEADER, false); // Do not return headers

curl_setopt($session, CURLOPT_RETURNTRANSFER, 0); // If you set this to 0, it will take you to a page with the http response



// Execute cURL session and close it



$response = curl_exec($session);

$httpcode = curl_getinfo($session, CURLINFO_HTTP_CODE);

echo "Code=>".$httpcode;

curl_close($session);

 

David J

Developer Portal

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

Visit Page

Constant Contact 2020 End of Summer Community Sweepstakes!

The Constant Contact User Community is hosting a sweepstakes. The more you participate, the more chances you have to win! Read on to learn more...

Read More
Featured