Reply
Occasional Visitor
anishm
Posts: 1
Registered: ‎05-27-2011

API Access error : The remote server returned (401) Unauthorized.

[ Edited ]

HI ,

 

 

I am trying to access the events though api, but it is returning some error .  My requirement is that , i need to list all events in my web site , please check  my code below

 

 


string sUsername = "*****";       

string sPassword = "*****";       

string sAPIKey = "*******";

 

string sUri2 = "https://api.constantcontact.com/ws/customers/" + sUsername.ToLower() + "/events";

 

Uri addy = new Uri(sUri2);       

HttpWebRequest request = WebRequest.Create(addy) as HttpWebRequest;       

request.Credentials = new NetworkCredential((sAPIKey + "%" + sUsername), sPassword);       

request.Method = "GET";       

request.Accept = "application/atom+xml";       

request.ContentType = "application/atom+xml";       

HttpWebResponse response = request.GetResponse() as HttpWebResponse;       

StreamReader srResponse = new StreamReader(response.GetResponseStream());       

string xml = srResponse.ReadToEnd().ToString();

 

 

CTCT Employee
David_J
Posts: 511
Registered: ‎06-04-2009

Re: API Access error : The remote server returned (401) Unauthorized.

I think this will do what you are looking for. Please let me know if this resolves your issue?

 

string sUsername = "YOUR USERNAME";
string sPassword = "YOUR PASSWORD";
string sAPIKey = "YOUR API KEY";

NetworkCredential loginInfo = new NetworkCredential(sAPIKey + "%" + sUsername, sPassword);

HttpWebRequest ctctRequest = (HttpWebRequest)WebRequest.Create("https://api.constantcontact.com/ws/customers/"+sUsername+"/events");
ctctRequest.Method = "GET";
ctctRequest.Credentials = loginInfo;
ctctRequest.Accept = "application/atom+xml";

HttpWebResponse ctctResponse = ctctRequest.GetResponse() as HttpWebResponse;
StreamReader responseStream = new StreamReader(ctctResponse.GetResponseStream());
string responseText = responseStream.ReadToEnd();
ctctResponse.Close();
responseStream.Close();
MessageBox.Show(responseText);

sadflkasdf

David J