Re: Unauthorized, StatusCode=401

Highlighted
Occasional Contributor

Re: Unauthorized, StatusCode=401

Hi Ryan,

 

How can i sync contacts from contactcontact to salesforce using apex class?

Does we need to use any API calls?

 

please reply

 

Thanks,

Narasimha

5 REPLIES 5
Highlighted
CTCT Employee

Re: Unauthorized, StatusCode=401

Hey,

 

You would definitely need to make several calls to our API but which ones would depend on what you are trying to sync and how much data you are looking to retrieve.  I would definitely read up on our documentation here.  We do have a salesforce application as well which you can download from with-in your account by clicking on Add Contacts and selecting CRM on the right hand side.  Depending on what you are looking to do, that may serve your needs.

 

Please let me know if this helps.

Ryan Davis
Quality Engineer
Highlighted
Occasional Contributor

Re: Unauthorized, StatusCode=401

 

public with sharing class ConstantContactAPI {
public void clickme()
{
String strurl='https://api.constantcontact.com/ws/customers/xxxxxxx@gmail.com/lists/1/members';
HttpRequest req=new HttpRequest();
req.setEndpoint(strurl);
req.setMethod('POST');
string strbody='Hi';
req.setBody(strbody);
req.setCompressed(true);
Http http=new Http();
HTTPResponse res=http.send(req);
String xmlData = res.getBody();
system.debug('**** XML Data is *****'+xmlData);
system.debug('***** Status is ****'+res.getStatus());
}
}

 

 

I used the above apex class, to connect to Constant contact API.

But its not working. I am getting an error " Unauthorized". 

Can you please correct the API URL.

Is this URL is correct? https://api.constantcontact.com/ws/customers/xxxxxxx@gmail.com/lists/1/members

Or should i use any other URL to sync contacts from constant contact to salesforce?

 


Highlighted
CTCT Employee

Re: Unauthorized, StatusCode=401

Hey,

 

That URI would return the first 50 members of your list with the list ID of 1 which is typically your general interest list.  If you are getting an unauthorized error you would be passing the wrong username and password into the account.  Or the concatenation of the apikey%username:password is not being setup correctly during the authentication.

 

I do not see where any username is passed into your request which is why it is showing up as a 401: unauthorized.

Ryan Davis
Quality Engineer
Highlighted
Occasional Contributor

Re: Unauthorized, StatusCode=401

Highlighted
CTCT Employee

Re: Unauthorized, StatusCode=401

Hey,

 

You would pass the information into credential method of some sort.  I am not familiar with APEX but I would definately check out their developers guide on how to do do webrequests.  That should point you in the direction of what you need to do to pass in credentials.  With out stating a username and password you wouldnt get access to your account.

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