Unauthorized, StatusCode=401

Highlighted
Occasional Contributor

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('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/USERNAME/contacts');
      req.setEndpoint('https://api.constantcontact.com/ws/customers/USERNAME/contacts');
      HttpResponse res = h.send(req);
      system.debug('********'+res.getbody());

}
}

 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

 

2 REPLIES 2
Highlighted
CTCT Employee

Re: Unauthorized, StatusCode=401

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
Highlighted
CTCT Employee

Re: Unauthorized, StatusCode=401

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

Constant Contact Would Like To Hear From You!

We want to hear from customers like you about your favorite features and how they have helped your business or organization. Tell us by answering a few questions in...

Read More
Featured