VBScript Requires Chilkat v11.0.0+
VBScript
Constant Contact V3: Add existing contacts to a list
See more Constant Contact Examples
Submit two existing contact IDs and one destination list ID. Constant Contact queues the change as a bulk activity. Save its ID, check the activity state and item errors, and resume checking that ID if the first few polls are inconclusive. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat VBScript Downloads
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)
success = 0
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set vault = CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
set tokens = CreateObject("Chilkat.JsonObject")
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
outFile.WriteLine("Run the desktop authorization example using this account and store.")
WScript.Quit
End If
If (tokens.JsonTypeOf("access_token") <> 1) Then
outFile.WriteLine("The saved authorization has no access token.")
WScript.Quit
End If
set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
http.AuthToken = tokens.StringOf("access_token")
set resp = CreateObject("Chilkat.HttpResponse")
' Use contact IDs from email lookups and the list ID from the selected destination.
set body = CreateObject("Chilkat.JsonObject")
success = body.UpdateString("source.contact_ids[0]","FIRST_CONTACT_ID")
success = body.UpdateString("source.contact_ids[1]","SECOND_CONTACT_ID")
success = body.UpdateString("list_ids[0]","DESTINATION_LIST_ID")
success = http.HttpJson("POST","https://api.cc.email/v3/activities/add_list_memberships",body,"application/json",resp)
If ((success <> 1) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
outFile.WriteLine("Could not start the membership activity. HTTP " & resp.StatusCode)
WScript.Quit
End If
set job = CreateObject("Chilkat.JsonObject")
success = job.Load(resp.BodyStr)
If (success <> 1) Then
outFile.WriteLine("Expected a JSON response.")
WScript.Quit
End If
If ((job.JsonTypeOf("activity_id") <> 1) Or (job.StringOfEquals("activity_id","",1) = 1)) Then
outFile.WriteLine("Missing activity_id in the response.")
WScript.Quit
End If
activityId = job.StringOf("activity_id")
outFile.WriteLine("Submitted activity ID: " & activityId)
success = http.SetUrlVar("job",activityId)
' Poll briefly. A longer-running job can be checked later using the same ID.
attempt = 0
Do While (attempt < 5)
success = http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",resp)
If ((success <> 1) Or (resp.StatusCode <> 200)) Then
outFile.WriteLine("Status check returned HTTP " & resp.StatusCode & ". Keep the activity ID and check again later.")
WScript.Quit
End If
success = job.Load(resp.BodyStr)
If ((success <> 1) Or (job.JsonTypeOf("state") <> 1)) Then
outFile.WriteLine("Missing activity state. Keep the activity ID.")
WScript.Quit
End If
state = job.StringOf("state")
If ((state <> "initialized") And (state <> "processing")) Then
outFile.WriteLine("Activity state: " & state)
i = 0
Do While (i < job.SizeOfArray("activity_errors"))
job.I = i
outFile.WriteLine(job.StringOf("activity_errors[i]"))
i = i + 1
Loop
WScript.Quit
End If
attempt = attempt + 1
If (attempt < 5) Then
http.SleepMs 2000
End If
Loop
outFile.WriteLine("Still processing. Recheck the activity ID before submitting another request.")
outFile.Close