C Requires Chilkat v11.0.0+
C
Constant Contact V3: Read every page of list members
See more Constant Contact Examples
Filter the contacts collection with lists and a list ID. To query multiple lists together, supply their comma-separated IDs. Follow the returned cursor links and report the total after the last page. The default contacts query excludes deleted records. A list member can still have unsubscribed email permission. Authorize the app first and use the same credential store.
Constant Contact developer guide
Chilkat C Downloads
#include <C_CkSecrets.h>
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkSecrets vault;
HCkJsonObject entry;
HCkJsonObject tokens;
BOOL loaded;
HCkHttp http;
HCkHttpResponse resp;
HCkStringBuilder url;
HCkJsonObject page;
int total;
int count;
int i;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
vault = CkSecrets_Create();
CkSecrets_putLocation(vault,"local_manager");
CkSecrets_putAutoSplit(vault,TRUE);
entry = CkJsonObject_Create();
CkJsonObject_UpdateString(entry,"appName","ConstantContactV3");
CkJsonObject_UpdateString(entry,"service","OAuth2");
CkJsonObject_UpdateString(entry,"username","my-account");
tokens = CkJsonObject_Create();
loaded = CkSecrets_GetSecretJson(vault,entry,tokens);
if (loaded != TRUE) {
printf("Run the desktop authorization example using this account and store.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
return;
}
if (CkJsonObject_JsonTypeOf(tokens,"access_token") != 1) {
printf("The saved authorization has no access token.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
return;
}
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putAuthToken(http,CkJsonObject_stringOf(tokens,"access_token"));
resp = CkHttpResponse_Create();
CkHttp_SetUrlVar(http,"list","LIST_ID");
url = CkStringBuilder_Create();
CkStringBuilder_Append(url,"https://api.cc.email/v3/contacts?lists={$list}&limit=50");
page = CkJsonObject_Create();
total = 0;
while ((CkStringBuilder_getLength(url) > 0)) {
success = CkHttp_HttpNoBody(http,"GET",CkStringBuilder_getAsString(url),resp);
if ((success != TRUE) || (CkHttpResponse_getStatusCode(resp) != 200)) {
printf("Expected HTTP 200. Received %d\n",CkHttpResponse_getStatusCode(resp));
if (CkHttpResponse_getStatusCode(resp) == 401) {
printf("Refresh the saved access token or authorize again.\n");
}
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(page);
return;
}
success = CkJsonObject_Load(page,CkHttpResponse_bodyStr(resp));
count = CkJsonObject_SizeOfArray(page,"contacts");
if ((success != TRUE) || (count < 0)) {
printf("Expected a page of contacts.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(page);
return;
}
i = 0;
while ((i < count)) {
CkJsonObject_putI(page,i);
printf("%s %s\n",CkJsonObject_stringOf(page,"contacts[i].contact_id"),CkJsonObject_stringOf(page,"contacts[i].email_address.address"));
i = i + 1;
}
total = total + count;
CkStringBuilder_Clear(url);
if (CkJsonObject_JsonTypeOf(page,"_links.next.href") == 1) {
CkStringBuilder_Append(url,CkJsonObject_stringOf(page,"_links.next.href"));
// The documented cursor link is relative to api.cc.email.
if (CkStringBuilder_StartsWith(url,"/v3/contacts?",TRUE) != TRUE) {
printf("Unexpected pagination link. Stopped before following it.\n");
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(page);
return;
}
CkStringBuilder_Prepend(url,"https://api.cc.email");
}
}
printf("List members read: %d\n",total);
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(url);
CkJsonObject_Dispose(page);
}