Add a Contact Using Classic ASP

jcmcmurr
Participant

Add a Contact Using Classic ASP

I've tried both of the examples from http://developer.constantcontact.com/node/341 but neither will work for me.  The first one gives a 401 and the 2nd (the class) gives a failed syntax error.  Is there any example code out there that works?

16 REPLIES 16
DaveBerard
Moderator

A 401 error message means that the code itself does indeed work.  Looking at this code, which appears to be an older sample, they problem appears to be simply the request URI doesn't start with https.  This will cause an authentication failure.


 


Here is an example I've used for testing in Classic ASP.  This is courtesy of another developer who was kind enough to give us permission to post it:


 


 


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="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= ""' Create your XML to post here or pass it in as part of the method

   

    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 "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

 
Dave Berard
Senior Product Manager, Constant Contact
ShaneJ2
Regular Participant

Dave,

 

  Thanks for the example.  That is exactly what I was looking for.  It looks like I just had something minor not quite right.  I should have it fixed in a few minutes.  Thanks!

 

Kris

SalS
Regular Participant

Kris,

 

Can you post how you implemented the asp code?  I don't know much about asp, but one of our clients has an asp website and I keep getting a "Bad Request!! Check Submitted Data:" error.  I've been combing through the code and can't figure out what's wrong with it.  Any help is appreciated.

 

Doug

JeffR680
Regular Participant

I have been going through the same code for the whole day and I could not get this to work.

 

Finally, I moved the code to a server windows 2003  which uses II6.0 and it works.  I have been working on Windows 7 which is using IIS 7. If you are using Windows 2008 it also uses IIS 7.

 

This also works on Windows XP which uses IIS 6. Below is my code that I used

 

Max

 

<%

    Dim FirstName                       '//Your Contact's First Name     Dim LastName                        '// Contact First name     Dim EmailAddress                    '//Your Contact's Email Address     Dim PhoneNumber                     '//Your Contact's phone Number     Dim Comments                        '//Your Contact's Comments     Dim UN                              '//Your Account Username (real one used)     Dim PW                              '//The Actoin Type, usually as listed     Dim OptInSource                     '//The Actoin Type, usually as listed     Dim API_Key                         '//Your API Key (real one used)     Dim List_ID                         '//Your Particular List ID,,1,2,3 & etc. (Default is 1, first user created would be 2, and so on)        ' ---- Operation:      '      POST -  add a new contact                       /contacts      '      GET  -  Retrieve all contacts for the account   /contacts      '      GET  -  Retrieve the entry for the contact      /contacts/{contact-id}      '      PUT  -  Update information for that contact     /contacts/{contact-id}      '      DELETE - delete the contact from all lists      /contacts/{contact-id}           FirstName = "Jane"      LastName = "Doe"      EmailAddress = "JaneDoe@pa.net"      PhoneNumber = "717-313-3948"      Comments = "Adding Jane to default mailing list"      UN = "username"      PW = "password"      OptInSource = "ACTION_BY_CUSTOMER"      API_Key = "e4fe0695-7f92-4588-bf19-as576793877"      List_ID = "1"

     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"">Contact: melodyo@pa.net</title>" & vbCrLf &_                     "<updated>"&UpdateTimeStamp&"</updated>" & vbcrlf &_                     "<author></author>" & vbcrlf &_                     "<id>https://api.constantcontact.com/ws/customers/ldsasoccer/contacts/101</id>" & vbcrlf &_                     "<summary type=""text"">Contact</summary>" & vbcrlf &_                     "<content type=""application/vnd.ctct+xml"">" & vbcrlf &_                         "<Contact xmlns=""http://ws.constantcontact.com/ns/1.0/"" id=""https://api.constantcontact.com/ws/customers/ldsasoccer/contacts/101"">" & 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"                                             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

JeffR680
Regular Participant

A follow up on Classic ASP for constant contact.  It will also work on IIS 7 but you have to create a separate application pool for the website that runs the script with the following: AspAppPool (32bits,integrated,no managed code)

 

Max

Hi Max,

 

Thank you for posting this for our developer community.  Have a wonderful day.

 

Best Regards,

Shannon W.

API Support Specialist

ShadT7
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.

ShadT7
Regular Participant

This code indeed does work!

 

Question now, how do you get an e-mail that is already in the system belonging to another list to update the lists it belongs to, to the one you are setting in this script as well?

 

When it checks to see if the contact is already in there, it is checking everything from REMOVE list, DO NOT MAIL list, ACTIVE list, etc.

 

Please advise.

 

Shad

To update a Contact that already exists (and we do check all those lists), you need to do a PUT request to the actual Contact ID (https://api.constantcontact.com/ws/customers/{username}/contacts/{id}.  You may need to do a GET or search to retrieve the Contact ID to update them if you don't know it already.  Here is documentation on our new JSON API on how to do this:  http://developer.constantcontact.com/docs/contacts-api/contacts-resource.html

 

I would recommend using the new JSON API instead of the old XML API.  It's both easier to work with and will have a longer life.

Dave Berard
Senior Product Manager, Constant Contact
ShadT7
Regular Participant

Can I use the JSON API in conjunction with the classic asp setup that I have though using the vAPI 1 authentication api key?

 

 

ShadT7
Regular Participant

Also this is confusing information.  I see all the test api, try it, and methods to use for JSON but it doesn't actually give you an example on how to implement.  Is there a skeleton example of adding/updating a contact with JSON?  Where does this code go, in a separate file of its own, or can you place inside existing syntax such as classic asp, etc.?

The way that it is implemented is very similar to the old V1 XML API, with the exception that we no longer support Basic authentication, you would need to use OAuth 2.0 authentication (simple header of "Authorization: Bearer {token here}").  The API is a REST API so you use verbs (GET, POST, PUT, DELETE) to take action on resources. 

 

We have many examples, wrappers and posts in PHP, C#, Java, iOS and Ruby.  Unfortunately, we do not have any examples of Classic ASP.  You can find sample code and wrappers on our github repo here: http://github.com/constantcontact

Dave Berard
Senior Product Manager, Constant Contact
ShadT7
Regular Participant

Dave,

 

Is there a time we can schedule a remote session so I can go over what I am trying to do here so it is clearer to the both of us?

Hello Shad,

 

It should be possible to schedule a meeting to discuss your goals. I will send you the details regarding that via email.

 

In regards to adding and updating contacts via classic ASP, the amount that we are able to do for you may be limited by the fact that we do not have any wrapper libraries or code that we provide for classic ASP. While code issues may present a challenge, we can definitely provide assistance on how to interact with the API.

 

Sincerely,

Elijah G.

API Support Specialist

Elijah G.
API Support Engineer
ShadT7
Regular Participant

That is perfect.  Do you have my e-mail address?

 

Thanks,

 

Shad

Hello Shad,

 

I do have your email, and I have sent you that message. Looking forward to hearing back from you.

 

Sincerely,

Elijah G.

API Support Specialist

Elijah G.
API Support Engineer
Developer Portal

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

Visit Page