cancel
Showing results for 
Search instead for 
Did you mean: 
Tomorrow morning (8/26) from 07:00 AM - 08:00 AM ET we need to make a few updates to our site. During this time, no emails will be sent and some customers will not be able to access their contacts. We recommend finishing up your work before 07:00 AM and logging in after 08:00 AM ET. Thank you for your patience while we make these updates.

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

Occasional Contributor

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('Authorization',authorizationHeader);
      req.setHeader('Content-length', '1753' );
     req.setHeader('Host','api.constantcontact.com');
    req.setHeader('Connection','keep-alive');
    req.setHeader('Content-Type', 'application/atom+xml');
     req.setMethod('GET');
      req.setbody('https://api.constantcontact.com/ws/customers/rajesh_y/contacts');
     string url='https://api.constantcontact.com/ws/customers/'+username+password+'/contacts/';
      req.setEndpoint(url);
      HttpResponse res = h.send(req);
      system.debug('********'+res.getbody());

}
}

 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

5 REPLIES 5
Moderator

Re: Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityConte

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
Occasional Contributor

Re: Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityConte

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?

 

 

 

 

Highlighted
Occasional Participant

Re: Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityConte

did anyone solve this issue??

CTCT Employee

Re: Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityConte

Hi Mahesh,

 

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

 

https://api.constantcontact.com/ws/customers/username/contacts/

 

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

Moderator

Re: Status=Unauthorized, StatusCode=401 An Authentication object was not found in the SecurityConte

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