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.

Unauthorized, StatusCode=401


Unauthorized, StatusCode=401

Hi all 


My code is

public class constantcontact 
public void conlist()
   Http h = new Http();
        HttpRequest req = new HttpRequest();
        final string username = 'USERNAME'; 
        final string password = 'PASSWORD';
        Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'BASIC ' +EncodingUtil.base64Encode(headerValue);
     //String authorizationHeader =username+':'+password;
      //req.setHeader('Content-length', '1753' );
    req.setHeader('Content-Type', 'application/atom+xml');
      HttpResponse res = h.send(req);


 I wanna get all contacts from constant contact 

but i am getting error like

Status=Unauthorized, StatusCode=401

An Authentication object was not found in the SecurityContext


Whats wrong inmy code please help me



Hey Rejeshy,


I am going to look at your code but it may take me some time.  I have modified it to hide your username and password.  I would highly suggest changing it as many people could have seen it. I will get back to you as soon as i find a solution for you.  Also i read in another post that you are trying to move your contacts into salesforce.  If you want to do so you can use the tool that we provide for this that is located in your account.  It is under CRM tools when you click on add and update contacts.  





Ryan Davis
Quality Engineer

Hi Rajesh,


I looked into your code which was written with Apex from salesforce which is a language I am not all to familiar with.  However I can see some errors in your code specifically with the connection.  Please take a look at my exampl of my connection string:


     String apikey = 'myapikey'; 
     String username = 'myname';
     String password = 'mypwd';
     Blob headerValue = Blob.valueOf(apikey + '%' + username + ':' + password);

This should fix your 401 error. 


Please let me know what the results are.


Ryan Davis
Quality Engineer
Developer Portal

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

Visit Page