Reply
Occasional Contributor
rajeshy
Posts: 2
Registered: ‎08-20-2011

Unauthorized, StatusCode=401

[ Edited ]

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

 

Ryan_D
Posts: 453
Topics: 0
Kudos: 20
Solutions: 21
Registered: ‎06-28-2011

Re: Unauthorized, StatusCode=401

[ Edited ]

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
Support Engineer, Constant Contact
Ryan_D
Posts: 453
Topics: 0
Kudos: 20
Solutions: 21
Registered: ‎06-28-2011

Re: Unauthorized, StatusCode=401

[ Edited ]

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
Support Engineer, Constant Contact