Classic ASP Sample Code Needed

Occasional Participant

Classic ASP Sample Code Needed

Hello all,

I'm trying to create a signup box and use the CC API to add contacts to a list.

Is there somewhere I can find some classic ASP sample code for this?

thanks in advance
Eric
20 REPLIES 20
Moderator

I'm not sure what Classic ASP Sample Code means, but we do have a very basic C# sample here that can be used in an ASP sign up form here. Hope this helps
Dave Berard
Senior Product Manager, Constant Contact
Occasional Participant

Sorry.

I mean asp/vbscript which was used before .Net came about.

and by sample code I meant a sample of how to add a contact.
Eric
Moderator

Unfortunately we do not have any VBScript code samples. The closest we offer is the C# example which shows how to use .NET classes ( converting to VB.NET is very easy).
Dave Berard
Senior Product Manager, Constant Contact
Frequent Participant

A sample was posted here.
Tom M
Group Product Manager – Content Editing
Constant Contact
Occasional Participant

wrote this up today. amalgamation of what worked in PHP and the ASP classic winHTTP methods. fully reusable function..

<%
FirstName="Contact_First_Name" //Your Contact's First Name
LastName="Contact_Last_Name" //Your Contact's Last Name
EmailAddress="Contact_Email" //Your Contact's Email Address
PostalCode="Contact_Zip_Code" //Your Contact's Zip Code
UN = "ConstantContact_User_Name" //Your Account Username
PW = "ConstantContact_Password" //Your Account Password
OptInSource="ACTION_BY_CUSTOMER" //The Actoin Type, usually as listed
API_Key = "ConstantContact_API_Key" //Your API Key
List_ID="1" //Your Particular List ID,,1,2,3 & etc. (Default is 1, first user created would be 2, and so on)




response.write(PostDataToURL(List_ID,FirstName,LastName,EmailAddress,PostalCode, "POST", UN,PW,API_Key))







Function PostDataToURL(List_ID,FirstName,LastName,EmailAddress,PostalCode, strMethod, UN,PW,API_Key)
Dim lngTimeout
Dim strUserAgentString
Dim intSslErrorIgnoreFlags
Dim blnEnableRedirects
Dim blnEnableHttpsToHttpRedirects
Dim strHostOverride
Dim strLogin
Dim strPassword
Dim strResponseText
Dim objWinHttp
Dim entry
Dim Base_URL
Dim UpdateTimeStamp
Base_URL="http://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 date()
strPostData="<entry xmlns=""http://www.w3.org/2005/Atom"">"&vbcrlf&_
"<title type=""text""> </title>"&vbcrlf&_
"<updated>"&UpdateTimeStamp&"</updated>"&vbcrlf&_
"<author></author>"&vbcrlf&_
"<id>data:,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>"&EmailAddress&"</EmailAddress>"&vbcrlf&_
"<FirstName>"&FirstName&"</FirstName>"&vbcrlf&_
"<LastName>"&LastName&"</LastName>"&vbcrlf&_
"<PostalCode>"&PostalCode&"</PostalCode>"&vbcrlf&_
"<OptInSource>"&OptInSource&"</OptInSource>"&vbcrlf&_
"<ContactLists>"&vbcrlf&_
"<ContactList id="""&Base_URL&"lists/"&List_ID&""" />"&vbcrlf&_
"</ContactLists>"&vbcrlf&_
"</Contact>"&vbcrlf&_
"</content>"&vbcrlf&_
"</entry>"


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"
PostDataToURL="2"&vbtab&":Contact Already Exists"
case "201"
PostDataToURL="1"&vbtab&":Contact Successfully added"
case "400"
PostDataToURL="0"&vbtab&":Bad Request. Check Submitted Data: "
case else
PostDataToURL = "0"&vbtab&":" & objWinHttp.Status & " " &objWinHttp.StatusText
end select
End If
Else
PostDataToURL = "Error " & Err.Number & " " & Err.Source & " " & _
Err.Description
End If
On Error GoTo 0
Set objWinHttp = Nothing
End Function


%>
Regular Participant

This is cool but I haven't been able to make it work. The problem may have more to do with the CC/Mashable auth business than how I'm trying to use the code. I already have an ASP page that imports downloaded web orders into our customer service database (none of this stufff was written by me). What I'd like to do is use this code to, while each order is being imported, stuff the customer info into a CC list for an auto-response email. I'm a programmer, but I don't have a lot of ASP experience, so I'm just figuring out things as I go along. But if I try to run this code as a test, it always fails with a 403 error. Which makes me think the API key/account info is what's causing it to fail. We're running IIS6, BTW.

 

This is basically just an internal bit of automation so that our customer service people don't have to query new order info into a spreadsheet and import that into a CC list.

 

Pete @ Clark Labs

Hi Pete,

 

The 403 is most often received when there is a mismatch between the username provided in the XML payload to us, and the username provided in the authentication details.

 

This code was written against our V1 API, which now has a limited shelf life (it will most likely be retired in 2014 or possibly later this year).  If you try to use a Mashery (V2) API key with code written for the V1 API, you will get a 401 error, so go to this page to generate a V1 API Key if you'd still like to use this example as a starting point.

 

Best Regards,

Shannon W.

API Support Specialist

I'm past the auth issue - my Base_URL needed https

 

Now I'm getting in but I think my XML is funky, I'm getting:

 

0 :Bad Request. Check Submitted Data

Hi Laurie,

 

See my response to your post here.  This XML was written a little outside of what I'd consider normal.  You probably want to either change the optinsource to action_by_customer, to move the contact to 'removed' status, or you want to add a contactList node so that the contact can actually be on a list.

 

Best Regards,

Shannon W.

API Support Specialist

Participant

OK, obviously this hasn't had a reply in quite a while but here goes:


I'm having an issue with the code posted here, and need some help.


Below is the full test page I'm using, and I'm continually getting a "code 0"/"case 400" error (Bad Request!!):


 <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<link href="/_css/styles.css" rel="stylesheet" type="text/css" />

<title>Blah Blah</title>

</head>

<body>

<%

FirstName= request.form("First_Name") '//Your Contact's First Name

LastName = request.form("Last_Name") '// Contact First name

EmailAddress=request.form("Email_Address") '//Your Contact's Email Address

PhoneNumber=request.form("Phone_Number") '//Your Contact's phone Number

Comments=request.form("Comments") '//Your Contact's Comments

UN = "xxxxxx" '//Your Account Username (real one used)

PW = "xxxxxx" '//Your Account Password (real one used)

OptInSource="ACTION_BY_CUSTOMER" '//The Actoin Type, usually as listed

API_Key = "xxxxxxxxxx" '//Your API Key (real one used)

List_ID="1" '//Your Particular List ID,,1,2,3 & etc. (Default is 1, first user created would be 2, and so on)


response.write(PostDataToURL(List_ID,FirstName,LastName,EmailAddress,PhoneNumber,Comments,"POST",UN,PW,API_Key))


Function PostDataToURL(List_ID,FirstName,LastName,EmailAddress,PhoneNumber,Comments,strMethod,UN,PW,API_Key)

Dim lngTimeout

Dim strUserAgentString

Dim intSslErrorIgnoreFlags

Dim blnEnableRedirects

Dim blnEnableHttpsToHttpRedirects

Dim strHostOverride

Dim strLogin

Dim strPassword

Dim strResponseText

Dim objWinHttp

Dim entry

Dim Base_URL

Dim UpdateTimeStamp

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 = date()

strPostData="<entry xmlns=""http://www.w3.org/2005/Atom"">"&vbcrlf&_

"<title type=""text""> </title>"&vbcrlf&_

"<updated>"&UpdateTimeStamp&"</updated>"&vbcrlf&_

"<author></author>"&vbcrlf&_

"<id>data:,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>"&EmailAddress&"</EmailAddress>"&vbcrlf&_

"<FirstName>"&FirstName&"</FirstName>"&vbcrlf&_

"<LastName>"&LastName&"</LastName>"&vbcrlf&_

"<PhoneNumber>"&PhoneNumber&"</PhoneNumber>"&vbcrlf&_

"<Comments>"&Comments&"</Comments>"&vbcrlf&_

"<OptInSource>"&OptInSource&"</OptInSource>"&vbcrlf&_

"<ContactLists>"&vbcrlf&_

"<ContactList id="""&Base_URL&"lists/"&List_ID&""" />"&vbcrlf&_

"</ContactLists>"&vbcrlf&_

"</Contact>"&vbcrlf&_

"</content>"&vbcrlf&_

"</entry>"


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"

PostDataToURL="2"&vbtab&":Contact Already Exists"

case "201"

PostDataToURL="1"&vbtab&":Contact Successfully added"

case "400"


'This is where the page errors out ALL the time...

PostDataToURL="0"&vbtab&":Bad Request!! Check Submitted Data: "

case else

PostDataToURL = "0"&vbtab&":" & objWinHttp.Status & " " &objWinHttp.StatusText

end select

End If

Else

PostDataToURL = "Error " & Err.Number & " " & Err.Source & " " & _

Err.Description

End If

On Error GoTo 0

Set objWinHttp = Nothing

End Function


%>

</body>

</html>


 


Previous page is passing everything correctly, as all variables are filling in right - here's the passing page's (include) code:


<form id="Form1" name="Form1" method="post" action="/includes/formNEW.asp">

<input type="text" id="First_Name" name="First_Name" maxlength="255" value="First Name" onfocus="this.select();" />

<input type="text" id="Last_Name" name="Last_Name" maxlength="255" value="Lastt Name" onfocus="this.select();" />

<input type="text" id="Email_Address" name="Email_Address" maxlength="255" value="E-mail address" onfocus="this.select();" />

<input type="text" id="Phone_Number" name="Phone_Number" maxlength="255" value="Phone Number" onfocus="this.select();" />

<textarea id="Comments" name="Comments" maxlength="255" onfocus="this.select();" />Your Message</textarea>

<input id="btnSubmit" name="btnSFSubmit" type="submit" value="" />

</form>


Anyone have any thoughts?  I'm at a loss, and client wants this DONE (as always)...  Thanks!

Employee

I have copied your sample code and ran it on my localhost and was able to use it to successfully create a few different contacts. The only modifications that I made were adding in my credentials, and changing to form action to the same page (as the form and PostDataToURL functions are on the same page for me). Your XML looks correct and and your page returned the message "1 :Contact Successfully added" as expected. I wish I could provide you with some more feedback, but I am not a VBScript expert and everything worked as intended.

David J

Regular Participant

Old thread, I know, but has anyone successfully implemented this in asp?  Could you please provide the code?  I'm getting the same error results using the code above, and I don't know enough about asp (particularly this WinHttp posting method) to figure it out.  I've been combing through the code for hours, scoured the forums, and got nothing.  I realize asp might be a dying language, but come on!

Regular Participant

I have been trying to make the above code work for the whole day and finally I realized the problem is not the code. The code works. 

 

I have been running the ASP code on IIS 7.x   It works perfectly on IIS6.0 but you can make it work on IIS 7 (Windows 7, Windows Vista, Windows 2008 server) if you create a separate application pool with the following charactertistics:  AspAppPool (32bits,integrated,no managed code) for the script you are running.

 

Max

 

I'll post a sample ASP code that adds, deletes and update a contact once I get this implemented.

 

 

Regular Participant

That would be fantastic!  Thank you! 

Regular Participant


@JeffR680 wrote:

I'll post a sample ASP code that adds, deletes and update a contact once I get this implemented.



Jeff, thank a million for sharing - any updates on this?

Regular Participant

This might help for users trying to do Post (new email), PUT (update email).  So, if an e-mail is already in the system this version will go GET the id of the e-mail then GET the contact lists it belongs to.  Once you have the contact lists they belong to you can add the additional list(s) you want them to belong to and update the contact.  If the e-mail does not exist in the system, then it simply does what others have referenced for the POST method.

 

http://community.constantcontact.com/t5/Getting-Started-with-API-s/Classic-ASP-asp-vbscript-GET-PUT-...

 

Trying to get the .asp script to attach to this forum post, so if it isn't there when you look, come back later as I'm currently on the phone with them trying to get an attachment to show.

Are you all set here?  Instead of attachments, we usually recommend hosting the file elsewhere (like you did on github) and then linking to it.

 

Best Regards,

Shannon W.

API Support Specialist

Regular Participant

Elijah embedded the code in the post.  Should be all set now.

Regular Participant

For the Classic ASP API, we did lists 1,2,3 etc..because that is how the "PUT" is done "list1,list2,list3" etc..it's not done by list id that I can see so somehow we have to coordinate the ids and names we are pulling to the form with the list indexes in the PUT. Ie, if you have 5 lists and they are list1,list2,list3,list4,list5 and then you delete list4 and add another list is it now

list1,list2,list3,list4,list5 or list1,list2,list3,list5,list6?  Does it reclaim the index?

In the past we used to just continue ti increment list IDs per contact. So if you have lists 1,2,3,4,5 delete 4, then add a new list, the new list would be ID 6.

 

However, this has been changed and we now assign a fully unique ID to every new list when it is created. Because of this, the best way to get a list ID is to make a GET request to the lists endpoint of the API so that you can get the ID there and use it in your code. If you simply need the list ID once and do not need to fetch it manually, you can make use of the I/O docs page that is now available through Mashery for testing API endpoints. You can find that here: https://constantcontact.mashery.com/io-docs

 

This page will allow you to take an API key and generate an Access Token for use with the API. Once you have the access token, you can run simple requests directly against the API from this page and view the results.

 

Best Regards,

Elijah G.
API Support Engineer
Developer Portal

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

Visit Page