Sample code for 30+ languages & platforms
Objective-C Requires Chilkat v11.0.0+

Constant Contact V3: Authorize a desktop app with PKCE

See more Constant Contact Examples

Register a V3 app for the public-client PKCE flow and register http://localhost:3017/ exactly, including the trailing slash. Chilkat opens the system browser, receives the redirect on the same computer, and exchanges the code using the PKCE verifier. Request contact_data for contacts and lists, campaign_data for email campaigns and reporting, and offline_access for refresh. If the app already has a contacts-only grant, repeat this browser authorization to grant campaign access. Refreshing the old token cannot add a scope. Use a client ID from the PKCE app registration.

The examples share a Chilkat Secrets record. Windows Credential Manager and Apple Keychain persist the local record. On systems where the local manager uses process memory, configure a persistent Secrets backend for all examples. Give each authorized account its own record name. If migrating from the old token-file example, authorize once to populate the store.

Constant Contact developer guide

Chilkat Objective-C Downloads

Objective-C
#import <CkoOAuth2.h>
#import <NSString.h>
#import <CkoJsonObject.h>
#import <CkoSecrets.h>
#import <CkoDateTime.h>

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
CkoOAuth2 *oauth = [[CkoOAuth2 alloc] init];
oauth.AuthorizationEndpoint = @"https://authz.constantcontact.com/oauth2/default/v1/authorize";
oauth.TokenEndpoint = @"https://authz.constantcontact.com/oauth2/default/v1/token";
oauth.ClientId = @"PKCE_CLIENT_ID";
oauth.LocalHost = @"localhost";
oauth.ListenPort = [NSNumber numberWithInt:3017];
//  Run authorization again if the account previously granted only contact access.
oauth.Scope = @"contact_data campaign_data offline_access";
oauth.CodeChallenge = YES;
oauth.CodeChallengeMethod = @"S256";
oauth.UseBasicAuth = NO;
NSString *url = [oauth StartAuth];
if (oauth.LastMethodSuccess != YES) {
    NSLog(@"%@",@"Could not start the local listener. Check the registered port.");
    return;
}

BOOL opened = [oauth LaunchBrowser: url];
if (opened != YES) {
    [oauth Cancel];
    NSLog(@"%@",@"Could not open the system browser.");
    return;
}

int waited = 0;
while (((waited < 300000) && ([oauth.AuthFlowState intValue] < 3))) {
    [oauth SleepMs: [NSNumber numberWithInt: 100]];
    waited = waited + 100;
}

if ([oauth.AuthFlowState intValue] < 3) {
    [oauth Cancel];
    NSLog(@"%@",@"Authorization timed out.");
    return;
}

if ([oauth.AuthFlowState intValue] == 4) {
    NSLog(@"%@",@"Authorization was declined.");
    return;
}

if ([oauth.AuthFlowState intValue] != 3) {
    NSLog(@"%@",@"Authorization failed. Check the PKCE app registration and exact redirect URI.");
    return;
}

CkoJsonObject *reply = [[CkoJsonObject alloc] init];
BOOL parsed = [reply Load: oauth.AccessTokenResponse];
if (([[reply JsonTypeOf: @"access_token"] intValue] != 1) || ([[reply IntOf: @"expires_in"] intValue] <= 0)) {
    NSLog(@"%@",@"Token response is missing an access token or lifetime.");
    return;
}

if ([reply StringOfEquals: @"access_token" value: @"" caseSensitive: YES] == YES) {
    NSLog(@"%@",@"The returned access token is empty.");
    return;
}

if (([[reply JsonTypeOf: @"refresh_token"] intValue] != 1) || ([reply StringOfEquals: @"refresh_token" value: @"" caseSensitive: YES] == YES)) {
    NSLog(@"%@",@"Missing refresh token. Check the offline_access scope and authorize again.");
    return;
}

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];
[tokens UpdateString: @"client_id" value: oauth.ClientId];
[tokens UpdateString: @"redirect_uri" value: @"http://localhost:3017/"];
[tokens UpdateString: @"refresh_token" value: [reply StringOf: @"refresh_token"]];
CkoDateTime *now = [[CkoDateTime alloc] init];
[now SetFromCurrentSystemTime];
[tokens UpdateString: @"obtained_at_utc" value: [now GetAsTimestamp: NO]];
[tokens UpdateString: @"access_token" value: [reply StringOf: @"access_token"]];
[tokens UpdateInt: @"expires_in" value: [reply IntOf: @"expires_in"]];
BOOL saved = [vault UpdateSecretJson: entry secret: tokens];
if (saved != YES) {
    NSLog(@"%@",@"Could not save the current token record. Check the credential store.");
    return;
}

NSLog(@"%@%d",@"Authorization saved. Access-token lifetime in seconds: ",[[tokens IntOf: @"expires_in"] intValue]);