Sample code for 30+ languages & platforms
Delphi DLL 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 Delphi DLL Downloads

Delphi DLL
var
oauth: HCkOAuth2;
url: PWideChar;
opened: Boolean;
waited: Integer;
reply: HCkJsonObject;
parsed: Boolean;
vault: HCkSecrets;
entry: HCkJsonObject;
tokens: HCkJsonObject;
now: HCkDateTime;
saved: Boolean;

begin
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oauth := CkOAuth2_Create();
CkOAuth2_putAuthorizationEndpoint(oauth,'https://authz.constantcontact.com/oauth2/default/v1/authorize');
CkOAuth2_putTokenEndpoint(oauth,'https://authz.constantcontact.com/oauth2/default/v1/token');
CkOAuth2_putClientId(oauth,'PKCE_CLIENT_ID');
CkOAuth2_putLocalHost(oauth,'localhost');
CkOAuth2_putListenPort(oauth,3017);
//  Run authorization again if the account previously granted only contact access.
CkOAuth2_putScope(oauth,'contact_data campaign_data offline_access');
CkOAuth2_putCodeChallenge(oauth,True);
CkOAuth2_putCodeChallengeMethod(oauth,'S256');
CkOAuth2_putUseBasicAuth(oauth,False);
url := CkOAuth2__startAuth(oauth);
if (CkOAuth2_getLastMethodSuccess(oauth) <> True) then
  begin
    Memo1.Lines.Add('Could not start the local listener. Check the registered port.');
    Exit;
  end;
opened := CkOAuth2_LaunchBrowser(oauth,url);
if (opened <> True) then
  begin
    CkOAuth2_Cancel(oauth);
    Memo1.Lines.Add('Could not open the system browser.');
    Exit;
  end;
waited := 0;
while ((waited < 300000) and (CkOAuth2_getAuthFlowState(oauth) < 3)) do
  begin
    CkOAuth2_SleepMs(oauth,100);
    waited := waited + 100;
  end;

if (CkOAuth2_getAuthFlowState(oauth) < 3) then
  begin
    CkOAuth2_Cancel(oauth);
    Memo1.Lines.Add('Authorization timed out.');
    Exit;
  end;
if (CkOAuth2_getAuthFlowState(oauth) = 4) then
  begin
    Memo1.Lines.Add('Authorization was declined.');
    Exit;
  end;
if (CkOAuth2_getAuthFlowState(oauth) <> 3) then
  begin
    Memo1.Lines.Add('Authorization failed. Check the PKCE app registration and exact redirect URI.');
    Exit;
  end;
reply := CkJsonObject_Create();
parsed := CkJsonObject_Load(reply,CkOAuth2__accessTokenResponse(oauth));
if ((CkJsonObject_JsonTypeOf(reply,'access_token') <> 1) or (CkJsonObject_IntOf(reply,'expires_in') <= 0)) then
  begin
    Memo1.Lines.Add('Token response is missing an access token or lifetime.');
    Exit;
  end;
if (CkJsonObject_StringOfEquals(reply,'access_token','',True) = True) then
  begin
    Memo1.Lines.Add('The returned access token is empty.');
    Exit;
  end;
if ((CkJsonObject_JsonTypeOf(reply,'refresh_token') <> 1) or (CkJsonObject_StringOfEquals(reply,'refresh_token','',True) = True)) then
  begin
    Memo1.Lines.Add('Missing refresh token. Check the offline_access scope and authorize again.');
    Exit;
  end;
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();
CkJsonObject_UpdateString(tokens,'client_id',CkOAuth2__clientId(oauth));
CkJsonObject_UpdateString(tokens,'redirect_uri','http://localhost:3017/');
CkJsonObject_UpdateString(tokens,'refresh_token',CkJsonObject__stringOf(reply,'refresh_token'));
now := CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(now);
CkJsonObject_UpdateString(tokens,'obtained_at_utc',CkDateTime__getAsTimestamp(now,False));
CkJsonObject_UpdateString(tokens,'access_token',CkJsonObject__stringOf(reply,'access_token'));
CkJsonObject_UpdateInt(tokens,'expires_in',CkJsonObject_IntOf(reply,'expires_in'));
saved := CkSecrets_UpdateSecretJson(vault,entry,tokens);
if (saved <> True) then
  begin
    Memo1.Lines.Add('Could not save the current token record. Check the credential store.');
    Exit;
  end;
Memo1.Lines.Add('Authorization saved. Access-token lifetime in seconds: ' + IntToStr(CkJsonObject_IntOf(tokens,'expires_in')));

CkOAuth2_Dispose(oauth);
CkJsonObject_Dispose(reply);
CkSecrets_Dispose(vault);
CkJsonObject_Dispose(entry);
CkJsonObject_Dispose(tokens);
CkDateTime_Dispose(now);