VB.NET Requires Chilkat v11.0.0+
VB.NET
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 VB.NET Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As New Chilkat.Secrets
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As New Chilkat.JsonObject
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
Dim tokens As New Chilkat.JsonObject
Dim loaded As Boolean = vault.GetSecretJson(entry,tokens)
If (loaded <> True) Then
Debug.WriteLine("Run the desktop authorization example using this account and store.")
Exit Sub
End If
If (tokens.JsonTypeOf("access_token") <> 1) Then
Debug.WriteLine("The saved authorization has no access token.")
Exit Sub
End If
Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
http.AuthToken = tokens.StringOf("access_token")
Dim resp As New Chilkat.HttpResponse
' Use contact IDs from email lookups and the list ID from the selected destination.
Dim body As New Chilkat.JsonObject
body.UpdateString("source.contact_ids[0]","FIRST_CONTACT_ID")
body.UpdateString("source.contact_ids[1]","SECOND_CONTACT_ID")
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 <> True) Or (resp.StatusCode < 200) Or (resp.StatusCode >= 300)) Then
Debug.WriteLine("Could not start the membership activity. HTTP " & resp.StatusCode)
Exit Sub
End If
Dim job As New Chilkat.JsonObject
success = job.Load(resp.BodyStr)
If (success <> True) Then
Debug.WriteLine("Expected a JSON response.")
Exit Sub
End If
If ((job.JsonTypeOf("activity_id") <> 1) Or (job.StringOfEquals("activity_id","",True) = True)) Then
Debug.WriteLine("Missing activity_id in the response.")
Exit Sub
End If
Dim activityId As String = job.StringOf("activity_id")
Debug.WriteLine("Submitted activity ID: " & activityId)
http.SetUrlVar("job",activityId)
' Poll briefly. A longer-running job can be checked later using the same ID.
Dim attempt As Integer = 0
While (attempt < 5)
success = http.HttpNoBody("GET","https://api.cc.email/v3/activities/{$job}",resp)
If ((success <> True) Or (resp.StatusCode <> 200)) Then
Debug.WriteLine("Status check returned HTTP " & resp.StatusCode & ". Keep the activity ID and check again later.")
Exit Sub
End If
success = job.Load(resp.BodyStr)
If ((success <> True) Or (job.JsonTypeOf("state") <> 1)) Then
Debug.WriteLine("Missing activity state. Keep the activity ID.")
Exit Sub
End If
Dim state As String = job.StringOf("state")
If ((state <> "initialized") And (state <> "processing")) Then
Debug.WriteLine("Activity state: " & state)
Dim i As Integer = 0
While (i < job.SizeOfArray("activity_errors"))
job.I = i
Debug.WriteLine(job.StringOf("activity_errors[i]"))
i = i + 1
End While
Exit Sub
End If
attempt = attempt + 1
If (attempt < 5) Then
http.SleepMs(2000)
End If
End While
Debug.WriteLine("Still processing. Recheck the activity ID before submitting another request.")