We all started somewhere! Share your experience on the Get Advice: Let's Get Started Sweepstakes thread and be entered to win a $100 credit on your Constant Contact account.

Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityContext


Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityContext

public class constantcontact 
public void conlist()
   Http h = new Http();
        HttpRequest req = new HttpRequest();
        final string username = 'ApiKey'.'%'.'Username'; 
        final string password = 'Password';
        Blob headerValue = Blob.valueOf(username+ ':' + password);
     //String authorizationHeader = 'BASIC ' +EncodingUtil.base64Encode(headerValue);
     String authorizationHeader ='Basic'+username+':'+password;
      req.setHeader('Content-length', '1753' );
    req.setHeader('Content-Type', 'application/atom+xml');
     string url='https://api.constantcontact.com/ws/customers/'+username+password+'/contacts/';
      HttpResponse res = h.send(req);


 Hi All


I want to get all contacts through http callouts 

all credentials are correct.

but i am getting error like

Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityContext



please help me in this area


It looks like the problem is that you are using the incorrect username in the url.  Instead of:


string url='https://api.constantcontact.com/ws/customers/'+username+password+'/contacts/';


You should have:


string url='https://api.constantcontact.com/ws/customers/'+UsernameWithoutApiKey+'/contacts/';


Also, I removed your password, API key and Username from the post to prevent account theft.  Please change your password immediately in case other people saw this post.

Dave Berard
Senior Product Manager, Constant Contact
Regular Participant

Hi Rajesh,


I am also working on the same scenario now.


Did u find the solution for this?


Can u please share this apex class to my mail id nara.dotnet@gmail.com?





Regular Participant

did anyone solve this issue??

Hi Mahesh,


This was caused by making a request to a URI with something other than just the username in the below URI:




Use the above URI, substituting only your Constant Contact username for "username" above.  Your API Key and password should be provided elsewhere, so don't insert those anywhere in the URI.


Best Regards,

Shannon W.

API Support Specialist

Sorry for never posting a solution to this.  The problem above was the manual setting of the authetication headers incorrectly.  Basic Authentication headers must be set following the defined protocol for HTTP requests, which is very well documented and a great example of how to do it can be found here: http://en.wikipedia.org/wiki/Basic_access_authentication#Protocol


Basically, the Authorization header should look similar to the following:


Authorization: Basic QWxhZGluOnNlc2FtIG9wZW4=


Above example set the header but did not Base64 encode the authentication string.  Encoding the string using the above code should get you authenticated using Basic authentication over Java.  As Shannon has pointed out in other threads, Basic authentication is depricated.  In future versions of the API, we will no longer support Basic Authentication and when we depricate the current version of the API, likely next year, there will be an end of life put on Basic Authentication.  We highly recommend taking the time now to understand and implement OAuth 2.0 now rather than have to implement Basic Authentication now and then learn OAuth 2.0 later on.

Dave Berard
Senior Product Manager, Constant Contact
Developer Portal

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

Visit Page