Objective-C Requires Chilkat v11.0.0+
Objective-C
Constant Contact V3: Find a campaign and its primary email activity
See more Constant Contact Examples
Enter the unique campaign name from the account. Follow the campaign collection’s cursor until the name matches, then fetch that campaign’s details. Select the activity whose role is primary_email. A permalink or resend can appear first.
The example writes the two IDs to newsletter-selection.json, the same file produced by the draft example. Run this lookup when selecting an existing newsletter for review or scheduling.
Authorize the application using the same account and credential store.
Constant Contact developer guide
Chilkat Objective-C Downloads
#import <CkoSecrets.h>
#import <CkoJsonObject.h>
#import <CkoHttp.h>
#import <CkoHttpResponse.h>
#import <NSString.h>
#import <CkoStringBuilder.h>
BOOL success = NO;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoSecrets *vault = [[CkoSecrets alloc] init];
vault.Location = @"local_manager";
vault.AutoSplit = YES;
CkoJsonObject *entry = [[CkoJsonObject alloc] init];
[entry UpdateString: @"appName" value: @"ConstantContactV3"];
[entry UpdateString: @"service" value: @"OAuth2"];
[entry UpdateString: @"username" value: @"my-account"];
CkoJsonObject *tokens = [[CkoJsonObject alloc] init];
BOOL loaded = [vault GetSecretJson: entry jsonOut: tokens];
if (loaded != YES) {
NSLog(@"%@",@"Run the desktop authorization example using this account and store.");
return;
}
if ([[tokens JsonTypeOf: @"access_token"] intValue] != 1) {
NSLog(@"%@",@"The saved authorization has no access token.");
return;
}
CkoHttp *http = [[CkoHttp alloc] init];
http.RequireSslCertVerify = YES;
http.AuthToken = [tokens StringOf: @"access_token"];
CkoHttpResponse *resp = [[CkoHttpResponse alloc] init];
NSString *campaignName = @"Workshop newsletter";
NSString *campaignId = @"";
CkoStringBuilder *url = [[CkoStringBuilder alloc] init];
[url Append: @"https://api.cc.email/v3/emails?limit=50"];
CkoJsonObject *page = [[CkoJsonObject alloc] init];
while ((([url.Length intValue] > 0) && (campaignId == @""))) {
success = [http HttpNoBody: @"GET" url: [url GetAsString] response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
NSLog(@"%@",@"Read campaigns: HTTP request failed.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d%@%@",@"Read campaigns: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
return;
}
success = [page Load: resp.BodyStr];
int count = [[page SizeOfArray: @"campaigns"] intValue];
if ((success != YES) || (count < 0)) {
NSLog(@"%@",@"Expected a page of campaigns.");
return;
}
int i = 0;
while (((i < count) && (campaignId == @""))) {
page.I = [NSNumber numberWithInt: i];
if ([page StringOfEquals: @"campaigns[i].name" value: campaignName caseSensitive: YES] == YES) {
campaignId = [page StringOf: @"campaigns[i].campaign_id"];
}
i = i + 1;
}
[url Clear];
if ((campaignId == @"") && ([[page JsonTypeOf: @"_links.next.href"] intValue] == 1)) {
[url Append: [page StringOf: @"_links.next.href"]];
if ([url StartsWith: @"/v3/emails?" caseSensitive: YES] != YES) {
NSLog(@"%@",@"Unexpected campaign pagination link.");
return;
}
[url Prepend: @"https://api.cc.email"];
}
}
if (campaignId == @"") {
NSLog(@"%@%@",@"No campaign named ",campaignName);
return;
}
[http SetUrlVar: @"campaign" value: campaignId];
success = [http HttpNoBody: @"GET" url: @"https://api.cc.email/v3/emails/{$campaign}" response: resp];
if ((success != YES) && ([resp.StatusCode intValue] <= 0)) {
NSLog(@"%@",@"Read campaign: HTTP request failed.");
return;
}
if ((success != YES) || ([resp.StatusCode intValue] != 200)) {
NSLog(@"%@%d%@%@",@"Read campaign: HTTP ",[resp.StatusCode intValue],@" ",resp.BodyStr);
return;
}
CkoJsonObject *campaign = [[CkoJsonObject alloc] init];
success = [campaign Load: resp.BodyStr];
if (success != YES) {
NSLog(@"%@",@"Expected a JSON object in the response.");
return;
}
// The campaign identifies the newsletter. The primary activity identifies the email to test or schedule.
CkoJsonObject *primary = [[CkoJsonObject alloc] init];
success = [campaign FindRecord2: @"campaign_activities" relPath: @"role" value: @"primary_email" caseSensitive: YES jsonObj: primary];
if ((success != YES) || ([[primary JsonTypeOf: @"campaign_activity_id"] intValue] != 1)) {
NSLog(@"%@",@"No primary email activity ID in the response.");
return;
}
NSString *selectedCampaign = [campaign StringOf: @"campaign_id"];
NSString *selectedActivity = [primary StringOf: @"campaign_activity_id"];
if ((selectedCampaign == @"") || (selectedActivity == @"")) {
NSLog(@"%@",@"Missing campaign or activity ID.");
return;
}
NSLog(@"%@%@",@"Campaign ID: ",selectedCampaign);
NSLog(@"%@%@",@"Primary activity ID: ",selectedActivity);
CkoJsonObject *selection = [[CkoJsonObject alloc] init];
[selection UpdateString: @"name" value: [campaign StringOf: @"name"]];
[selection UpdateString: @"campaign_id" value: selectedCampaign];
[selection UpdateString: @"campaign_activity_id" value: selectedActivity];
success = [selection WriteFile: @"newsletter-selection.json"];
if (success != YES) {
NSLog(@"%@",@"Could not save newsletter-selection.json. Keep the IDs printed above.");
return;
}
NSLog(@"%@",@"Saved newsletter-selection.json.");