Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

Highlighted
Participant

Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

I've worked up a working version for the v1 Constant Contact API that does GET, PUT, POST Methods for a new e-mail address to a contact list and updating an e-mail address that already exists in the system to be added to the designated contact list.  There are a few forums that helped in this matter that are located here:

 

http://community.constantcontact.com/t5/Developer-Support-ask-questions/Add-a-Contact-Using-Classic-...

 

http://community.constantcontact.com/t5/Getting-Started-with-API-s/Classic-ASP-Sample-Code-Needed/td... 

 

I've attached the sample code in a .asp file within a .zip file for convenience.  This code exceeds the 50,000 character limit to be able to post correctly. 

 

The attached version also contains commented text to help understand further. Hope this helps anyone that needs or requires a Classic ASP implementation.

 

Hope this helps you all!

 

<!--******************************************************************
* Code Provided By: Shad N. Taylor
* Project Purpose: Connect Web Form to specific Constant Contact
*                  list via POST, GET, PUT Methods
* Syntax: Classic ASP
* Questions: Please e-mail shad-taylor@peddinghaus.com
* Date: 07/30/2013
*******************************************************************-->

<%
eMail = "test100@test100.com" 'Email address coming from the webform that is being submitted

constantContactAPI eMail 'Call the SUB passing the eMail from the webform

'------------------------------------------------------------------------------------'
' SUB: constantContactAPI
' DEFINITION: Used to add a new E-mail address and Update an E-mail address
' CALLS: callUpdateEmailAddress
' VARS: eMail, UN, PW, OptInSource, API_Key, List_ID
'------------------------------------------------------------------------------------'	
SUB constantContactAPI(eMail)
 	UN = "YOURUSERNAME" 'Your Account Username
 	PW = "YOURPASSWORD" 'Your Account Password
 	OptInSource = "ACTION_BY_CUSTOMER" 'The Action Type (ACTION_BY_CUSTOMER OR ACTION_BY_CONTACT) 
 	API_Key = "YOURAPIKEY" 'Your API Key - v1 version
 	List_ID = "YOURLISTID" 'Default ID for list
	
    response.write (callAddContact(List_ID,eMail,OptInSource,"POST",UN,PW,API_Key)) 'Calling Function

END SUB 'constantContactAPI

'------------------------------------------------------------------------------------'
' FUNCTION: callAddContact
' DEFINITION: Used to add a new E-mail address and Update an E-mail address
' CALLS: callUpdateEmailAddress
' VARS: List_ID, eMail, OptInSource, strMethod, UN, PW, API_Key
'------------------------------------------------------------------------------------'	
FUNCTION callAddContact(List_ID,eMail,OptInSource,strMethod,UN,PW,API_Key)
	 Dim lngTimeout, strUserAgentString, intSslErrorIgnoreFlags, blnEnableRedirects, blnEnableHttpsToHttpRedirects
	 Dim strHostOverride, strLogin, strPassword, strResponseText, objWinHttp
	 Dim entry, Base_URL, UpdateTimeStamp 'Create your variables

	 Base_URL="https://api.constantcontact.com/ws/customers/"& UN &"/"

	 lngTimeout = 59000
	 strUserAgentString = "http_requester/0.1"
	 intSslErrorIgnoreFlags = 0 '13056: ignore all err, 0: accept no err
	 blnEnableRedirects = True
	 blnEnableHttpsToHttpRedirects = True
	 strHostOverride = ""
	 strLogin = API_Key & "%" & UN 'Login format for v1
	 strPassword = PW
	 UpdateTimeStamp = now() 'Current date for when the contact was added or updated
	 
    'XML to POST
	 strPostData="<entry xmlns=""http://www.w3.org/2005/Atom""> "&vbcrlf&_
	 "<title type=""text""> </title> "&vbcrlf&_
	 "<updated>"& UpdateTimeStamp &"</updated>" &vbcrlf&_
	 "<author></author>" &vbcrlf&_
	 "<id>data&colon;,none</id>" &vbcrlf&_
	 "<summary type=""text"">Contact</summary>" &vbcrlf&_
	 "<content type=""application/vnd.ctct+xml"">" &vbcrlf&_
	 "<Contact xmlns=""http://ws.constantcontact.com/ns/1.0/"">" &vbcrlf&_
	 "<EmailAddress>"& eMail &"</EmailAddress>" &vbcrlf&_
	 "<FirstName></FirstName>" &vbcrlf&_
	 "<LastName></LastName>" &vbcrlf&_
	 "<PostalCode></PostalCode>" &vbcrlf&_
	 "<OptInSource>"& OptInSource &"</OptInSource>" &vbcrlf&_
	 "<ContactLists>" &vbcrlf&_
	 "<ContactList id="""& Base_URL &"lists/"& List_ID &""" />" &vbcrlf&_
	 "</ContactLists>" &vbcrlf&_
	 "</Contact>" &vbcrlf&_
	 "</content>" &vbcrlf&_
	 "</entry>"

    'POST to Constant Contact URL
	 Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
	 objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
	 objWinHttp.Open strMethod,Base_URL&"contacts"
	 
	 If strMethod = "POST" Then
		 objWinHttp.setRequestHeader "Content-type", _
		 "application/atom+xml"
	 End If
	 
	 If strHostOverride <> "" Then
	 	objWinHttp.SetRequestHeader "Host", strHostOverride
	 End If
	 
		 objWinHttp.Option(0) = strUserAgentString
		 objWinHttp.Option(4) = intSslErrorIgnoreFlags
		 objWinHttp.Option(6) = blnEnableRedirects
		 objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects
	 
	 If (strLogin <> "") And (strPassword <> "") Then
	 	objWinHttp.SetCredentials strLogin, strPassword, 0
	 End If 
	 
	 On Error Resume Next
	 
	 objWinHttp.Send(strPostData)
	
	 If Err.Number = 0 Then
	 
	 	If objWinHttp.Status = "200" Then
	 		PostDataToURL = objWinHttp.ResponseText
	 	Else

	         SELECT CASE objWinHttp.Status
		         CASE "409" '409 Status means the contact already exists, now we need to update the contact for this list
	                response.write (callUpdateEmailAddress("GET", UN, PW, API_Key, eMail, List_ID))	'Calling Function
                 
                 CASE "201" '201 Status means the contact was added to the Constant Contact list
		 	        response.write "New E-mail was added"
		         
                 CASE "400" '400 Status means there was issues adding the contact or updating the contact
		 	        response.write "Email was not added or updated"
		         
                 CASE ELSE
			        response.write "Email was not added or updated"
             END SELECT
         END IF

	 Else
	 	response.write "Sometype of Error on Script Prevented us from doing anything"
	 End If
	 
	 On Error GoTo 0
     
     Set objWinHttp = Nothing
END FUNCTION 'callAddContact

'------------------------------------------------------------------------------------'
' FUNCTION: callUpdateEmailAddress
' DEFINITION: Used to update an E-mail address
' CALLS: None
' VARS: strMethod, UN, PW, API_Key, eMail
'------------------------------------------------------------------------------------'	
FUNCTION callUpdateEmailAddress(strMethod, UN, PW, API_Key, eMail, List_ID)
	 Dim lngTimeout, strUserAgentString, intSslErrorIgnoreFlags, blnEnableRedirects, blnEnableHttpsToHttpRedirects
	 Dim strHostOverride, strLogin, strPassword, strResponseText, objWinHttp
	 Dim entry, Base_URL, UpdateTimeStamp 'Create your variables

	 Base_URL="https://api.constantcontact.com/ws/customers/"& UN &"/"

	 lngTimeout = 59000
	 strUserAgentString = "http_requester/0.1"
	 intSslErrorIgnoreFlags = 0 '13056: ignore all err, 0: accept no err
	 blnEnableRedirects = True
	 blnEnableHttpsToHttpRedirects = True
	 strHostOverride = ""
	 strLogin = API_Key & "%" & UN
	 strPassword = PW
	 UpdateTimeStamp = now()
     eMail = replace(eMail, "@", "%40") 'Need to reformat the email address for the PUT method replacing @ with %40
    
     'GET to Constant Contact URL - We need to find the ID of the eMail in the system
  	 Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
	 objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
	 objWinHttp.Open strMethod,Base_URL&"contacts?email="& eMail &""
	 
	 If strMethod = "GET" Then
		 objWinHttp.setRequestHeader "Content-type", _
		 "application/atom+xml"
	 End If
	 
	 If strHostOverride <> "" Then
	 	objWinHttp.SetRequestHeader "Host", strHostOverride
	 End If
	 
		 objWinHttp.Option(0) = strUserAgentString
		 objWinHttp.Option(4) = intSslErrorIgnoreFlags
		 objWinHttp.Option(6) = blnEnableRedirects
		 objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects
	 
	 If (strLogin <> "") And (strPassword <> "") Then
	 	objWinHttp.SetCredentials strLogin, strPassword, 0
	 End If 
	 
	 On Error Resume Next
	 objWinHTTP.Send 
   
     'Create XML to parse from the response
     getIDXML = objWinHttp.ResponseText
	    
        'XML parser - Find the ID URL
		Dim xml: Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0") 
		xml.LoadXml(""& getIDXML &"")
	
		Dim sResult: sResult = ""
	
		Dim node
		For Each node in xml.selectSingleNode("/feed/entry/id").childNodes
			sResult = sResult & node.xml
		Next
	
        'Convert URL to HTTPS that holds the ID of the eMail for the next GET method
		myCCListURL = replace(sResult, "http://", "https://")
    
        'Clear everything out for the next method call
        Set xml = Nothing
        Set node = Nothing
        Set sResult = Nothing
        Set objWinHTTP = Nothing
	
	'GET to Constant Contact URL - We need to find the contact lists this ID belongs to
	 Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
	 objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
	 objWinHttp.Open strMethod,""& myCCListURL &""

	 If strMethod = "GET" Then
		 objWinHttp.setRequestHeader "Content-type", _
		 "application/atom+xml"
	 End If
	 
	 If strHostOverride <> "" Then
	 	objWinHttp.SetRequestHeader "Host", strHostOverride
	 End If
	 
		 objWinHttp.Option(0) = strUserAgentString
		 objWinHttp.Option(4) = intSslErrorIgnoreFlags
		 objWinHttp.Option(6) = blnEnableRedirects
		 objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects
	 
	 If (strLogin <> "") And (strPassword <> "") Then
	 	objWinHttp.SetCredentials strLogin, strPassword, 0
	 End If 
	 
	 On Error Resume Next
	 objWinHTTP.Send 

    'Create XML to parse from the response
	 getContactListXML = objWinHttp.ResponseText
        
        'XML parser - Find the Contact Lists nodes
        Set xml = Server.CreateObject("MSXML2.DOMDocument.3.0") 
		xml.LoadXml(""&  getContactListXML &"")
	
		Dim xmlContactLists: xmlContactLists = ""
	
		For Each node in xml.selectSingleNode("/entry/content/Contact/ContactLists").childNodes
			xmlContactLists = xmlContactLists & node.xml
		Next
	    
       'Clear everything out for the next method call
        Set xml = Nothing
        Set node = Nothing
        Set objWinHTTP = Nothing

        'Need to modify the response to clear out XMLNS reference for the PUT method 
        xmlContactLists = replace(xmlContactLists, " xmlns=""http://ws.constantcontact.com/ns/1.0/""", "")
	  

    strMethod = "PUT" 'Change the Method to a PUT now that we have everything we need to insert
    myCCListURL = replace(myCCListURL, "https://", "http://") 'Temporarily change URL back to HTTP
    myHref = replace(myCCListURL, "http://api.constantcontact.com", "") '<link href="/ws/...."> format
    myEmail = replace(eMail, "%40", "@") 'Create variable for the eMail to have @ for insertion

    'Concatanate the XML for insertion with topXML, contactList, addList, bottomXML
    'topXML: The XML format all the way to the ContactLists node
    'contactList: Insert xmlContactLists from the GET method
    'addList: List you want the contact to be updated to
    'bottomXML: The XML format from the end of ContactLists node to the end of the XML file

     topXML = "<?xml version=""1.0"" encoding=""UTF-8""?>" &vbcrlf&_
     "<entry xmlns=""http://www.w3.org/2005/Atom"">" &vbcrlf&_
     "<link href="""&  myHref &""" rel=""edit"" />" &vbcrlf&_
     "<id>"& myCCListURL &"</id> " &vbcrlf&_
     "<title type=""text"">Contact: "& myEmail &"</title>" &vbcrlf&_
     "<updated>"& UpdateTimeStamp &"</updated>" &vbcrlf&_
     "<author>" &vbcrlf&_
     "<name>"& myEmail &"</name>" &vbcrlf&_
     "</author>" &vbcrlf&_
     "<content type=""application/vnd.ctct+xml"">" &vbcrlf&_
     "<Contact xmlns=""http://ws.constantcontact.com/ns/1.0/"" id="""& myCCListURL &""">" &vbcrlf&_
     "<OptInSource>ACTION_BY_CONTACT</OptInSource>" &vbcrlf&_
     "<Status>Active</Status>" &vbcrlf&_
     "<EmailAddress>"& myEmail &"</EmailAddress>" &vbcrlf&_
     "<EmailType>HTML</EmailType>" &vbcrlf&_
     "<Name>"& myEmail &"</Name> " &vbcrlf&_
     "<FirstName></FirstName>" &vbcrlf&_
     "<MiddleName></MiddleName>" &vbcrlf&_
     "<LastName></LastName>" &vbcrlf&_
     "<JobTitle></JobTitle>" &vbcrlf&_
     "<CompanyName></CompanyName>" &vbcrlf&_
     "<HomePhone></HomePhone>" &vbcrlf&_
     "<WorkPhone></WorkPhone>" &vbcrlf&_
     "<Addr1></Addr1>" &vbcrlf&_
     "<Addr2></Addr2>" &vbcrlf&_
     "<Addr3></Addr3>" &vbcrlf&_
     "<City></City>" &vbcrlf&_
     "<StateCode></StateCode>" &vbcrlf&_
     "<StateName></StateName>" &vbcrlf&_
     "<CountryCode></CountryCode>" &vbcrlf&_
     "<CountryName></CountryName>" &vbcrlf&_
     "<PostalCode></PostalCode>" &vbcrlf&_
     "<SubPostalCode></SubPostalCode>" &vbcrlf&_
     "<Note></Note>" &vbcrlf&_
     "<CustomField1></CustomField1>" &vbcrlf&_
     "<CustomField2></CustomField2>" &vbcrlf&_
     "<CustomField3></CustomField3>" &vbcrlf&_
     "<CustomField4></CustomField4>" &vbcrlf&_
     "<CustomField5></CustomField5>" &vbcrlf&_
     "<CustomField6></CustomField6>" &vbcrlf&_
     "<CustomField7></CustomField7>" &vbcrlf&_
     "<CustomField8></CustomField8>" &vbcrlf&_
     "<CustomField9></CustomField9>" &vbcrlf&_
     "<CustomField10></CustomField10>" &vbcrlf&_
     "<CustomField11></CustomField11>" &vbcrlf&_
     "<CustomField12></CustomField12>" &vbcrlf&_
     "<CustomField13></CustomField13>" &vbcrlf&_
     "<CustomField14></CustomField14>" &vbcrlf&_
     "<CustomField15></CustomField15>"&vbcrlf&_
     "<ContactLists>"

     contactList= xmlContactLists
  
     addList = "<ContactList id=""http://api.constantcontact.com/ws/customers/"& UN &"/lists/"& List_ID &""">" &vbcrlf&_
     "<link xmlns=""http://www.w3.org/2005/Atom"" href=""/ws/customers/"& UN &"/lists/"& List_ID &""" rel=""self""></link>" &vbcrlf&_
     "<OptInSource>ACTION_BY_CONTACT</OptInSource>" &vbcrlf&_
     "<OptInTime>"& UpdateTimeStamp &"</OptInTime>" &vbcrlf&_
     "</ContactList>"
 
     bottomXML = "</ContactLists>"&vbcrlf&_
     "<Confirmed>true</Confirmed>"&vbcrlf&_
     "<InsertTime>"& UpdateTimeStamp &"</InsertTime>"&vbcrlf&_
     "<LastUpdateTime>"& UpdateTimeStamp &"</LastUpdateTime>"&vbcrlf&_
     "</Contact>"&vbcrlf&_
     "</content>"&vbcrlf&_
     "<source>"&vbcrlf&_
     "<id>http://api.constantcontact.com/ws/customers/"& UN &"/contacts</id>"&vbcrlf&_
     "<title type=""text"">Contacts for Customer: "& UN &"</title>"&vbcrlf&_
     "<link href=""contacts"" />"&vbcrlf&_
     "<link href=""contacts"" rel=""self"" />"&vbcrlf&_
     "<author>"&vbcrlf&_
     "<name>"& UN &"</name>"&vbcrlf&_
     "</author>"&vbcrlf&_
     "<updated>"& UpdateTimeStamp &"</updated>"&vbcrlf&_
     "</source>"&vbcrlf&_
     "</entry>"

    'Concatanate the XML for insertion
    fullXML = topXML + contactList + addList + bottomXML

    'Convert URL back to HTTPS for method call
    myCCListURL = replace(myCCListURL, "http://", "https://")

    'PUT to Constant Contact URL - We need to insert the newly created Concatanated XML file
    Set objWinHttp = Server.CreateObject("WinHttp.WinHttpRequest.5.1")
    objWinHttp.SetTimeouts lngTimeout, lngTimeout, lngTimeout, lngTimeout
    objWinHttp.Open strMethod,""& myCCListURL &""

	 If strMethod = "PUT" Then
		 objWinHttp.setRequestHeader "Content-type", _
		 "application/atom+xml"
	 End If

	 If strHostOverride <> "" Then
	 	objWinHttp.SetRequestHeader "Host", strHostOverride
	 End If
	 
		 objWinHttp.Option(0) = strUserAgentString
		 objWinHttp.Option(4) = intSslErrorIgnoreFlags
		 objWinHttp.Option(6) = blnEnableRedirects
		 objWinHttp.Option(12) = blnEnableHttpsToHttpRedirects
	 
	 If (strLogin <> "") And (strPassword <> "") Then
	 	objWinHttp.SetCredentials strLogin, strPassword, 0
	 End If 
	 
	 On Error Resume Next

	 objWinHttp.Send(fullXML)
	 
     IF objWinHttp.Status = "204" THEN '204 Status means the contact was updated successfully
         response.write "SUCCESS: Updated E-mail"
     ELSE 'Was not able to update the contact
	 	 response.write "Error: "& objWinHttp.Status &"<br><br>Was not able to update e-mail address"
     END IF
	 
	 Set objWinHttp = Nothing

END FUNCTION 'callUpdateEmailAddress
 %>

 

10 REPLIES 10
Highlighted
Employee

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

Thank you for posting this!  We very much appreciate the work you did, and for sharing it to help others.

 

Best Regards,

Shannon W.

API Support Specialist

Highlighted
Participant

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

No problem. 

Highlighted
Occasional Participant

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

First, thank you ShadT7!  However, I put this on a standalone asp page, and entered a test email address and my cc info at the top, and when I ran the page I got "Email was not added or updated".  My understanding is it should work like that... is there any kind of special implementation needed?

 

CC people, can we put a paid programmer on this for a day to make this an official ASP classic solution?  Maybe with some examples of implementation?

Highlighted
Participant

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

Did you put a v1 API Key in?

 

Send your solution to shad-taylor@peddinghaus.com and I'll take a look at it.  Also, Elijah from webservices has worked with me on this so he is well versed right now on how to get this to correctly run.

 

I tested the script before posting and all is in tact.

Highlighted
Occasional Participant

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

It looks like this code only

  1. Adds Subscriptions
  2. Changes Emails

Is there something that gets the real-time subscription status from Constant Contact? When someone edits their subscription on my site, I want them to see what lists on CC they are subscribed to.

Highlighted
Honored Contributor

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

Hello Nick,

 

You absolutely can use the API to get information such as what lists a contact is subscribed to. In order to do this, you would need to write code that could perform a GET request to that particular contact and parse the XML response to get the List IDs that they are assigned to, and then perform a GET to each list to get the name.

 

If this is soemthing that you would feel comfortable doing, you would need to work with these endpoints:

 

Contact Search (To get the contact ID): https://community.constantcontact.com/t5/Documentation/Searching-for-a-Contact-by-Email-Address/ba-p...

Contact Details: https://community.constantcontact.com/t5/Documentation/Obtaining-a-Contact-s-Information/ba-p/25057

List Details: https://community.constantcontact.com/t5/Documentation/Retrieving-an-Individual-List/ba-p/25065

 

If you do work on implementing this, please feel free to reach out to us for assitance with understanding and working with the API.

 

Best Regards,

Elijah G.

API Support Engineer

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

The older working classic ASP examples no longer seem to work with Constant Contact. Using v1 Key

Highlighted
Honored Contributor

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

Hello,

 

The older code on the V1 API should still be functional. If you were attempting to use these codes on November 17th, I suspect that you may have been impacted by a system issue that we experienced. If you try the code again, you may see different results.

 

Best Regards,

Elijah G.
API Support Engineer
Highlighted
Occasional Participant

Re: Classic ASP - asp/vbscript GET, PUT, POST Methods - v1 Constant Contact API

Still no joy using classic ASP (any example) with V1 or V2 key. Can you post an example that still works using classic ASP?

Developer Portal

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

Visit Page

Register for our "Making Sense of Online Marketing for Retail" Webinar

Join us on Thursday, August 13th at 2pm ET for a free webinar where we'll discuss how to incorporate online marketing into your retail store.

Read More
Featured