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

Unicode C
#include <C_CkOAuth2W.h>
#include <C_CkJsonObjectW.h>
#include <C_CkSecretsW.h>
#include <C_CkDateTimeW.h>

void ChilkatSample(void)
    {
    HCkOAuth2W oauth;
    const wchar_t *url;
    BOOL opened;
    int waited;
    HCkJsonObjectW reply;
    BOOL parsed;
    HCkSecretsW vault;
    HCkJsonObjectW entry;
    HCkJsonObjectW tokens;
    HCkDateTimeW now;
    BOOL saved;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    oauth = CkOAuth2W_Create();
    CkOAuth2W_putAuthorizationEndpoint(oauth,L"https://authz.constantcontact.com/oauth2/default/v1/authorize");
    CkOAuth2W_putTokenEndpoint(oauth,L"https://authz.constantcontact.com/oauth2/default/v1/token");
    CkOAuth2W_putClientId(oauth,L"PKCE_CLIENT_ID");
    CkOAuth2W_putLocalHost(oauth,L"localhost");
    CkOAuth2W_putListenPort(oauth,3017);
    //  Run authorization again if the account previously granted only contact access.
    CkOAuth2W_putScope(oauth,L"contact_data campaign_data offline_access");
    CkOAuth2W_putCodeChallenge(oauth,TRUE);
    CkOAuth2W_putCodeChallengeMethod(oauth,L"S256");
    CkOAuth2W_putUseBasicAuth(oauth,FALSE);
    url = CkOAuth2W_startAuth(oauth);
    if (CkOAuth2W_getLastMethodSuccess(oauth) != TRUE) {
        wprintf(L"Could not start the local listener. Check the registered port.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    opened = CkOAuth2W_LaunchBrowser(oauth,url);
    if (opened != TRUE) {
        CkOAuth2W_Cancel(oauth);
        wprintf(L"Could not open the system browser.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    waited = 0;
    while (((waited < 300000) && (CkOAuth2W_getAuthFlowState(oauth) < 3))) {
        CkOAuth2W_SleepMs(oauth,100);
        waited = waited + 100;
    }

    if (CkOAuth2W_getAuthFlowState(oauth) < 3) {
        CkOAuth2W_Cancel(oauth);
        wprintf(L"Authorization timed out.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    if (CkOAuth2W_getAuthFlowState(oauth) == 4) {
        wprintf(L"Authorization was declined.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    if (CkOAuth2W_getAuthFlowState(oauth) != 3) {
        wprintf(L"Authorization failed. Check the PKCE app registration and exact redirect URI.\n");
        CkOAuth2W_Dispose(oauth);
        return;
    }

    reply = CkJsonObjectW_Create();
    parsed = CkJsonObjectW_Load(reply,CkOAuth2W_accessTokenResponse(oauth));
    if ((CkJsonObjectW_JsonTypeOf(reply,L"access_token") != 1) || (CkJsonObjectW_IntOf(reply,L"expires_in") <= 0)) {
        wprintf(L"Token response is missing an access token or lifetime.\n");
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    if (CkJsonObjectW_StringOfEquals(reply,L"access_token",L"",TRUE) == TRUE) {
        wprintf(L"The returned access token is empty.\n");
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    if ((CkJsonObjectW_JsonTypeOf(reply,L"refresh_token") != 1) || (CkJsonObjectW_StringOfEquals(reply,L"refresh_token",L"",TRUE) == TRUE)) {
        wprintf(L"Missing refresh token. Check the offline_access scope and authorize again.\n");
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(reply);
        return;
    }

    vault = CkSecretsW_Create();
    CkSecretsW_putLocation(vault,L"local_manager");
    CkSecretsW_putAutoSplit(vault,TRUE);
    entry = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(entry,L"appName",L"ConstantContactV3");
    CkJsonObjectW_UpdateString(entry,L"service",L"OAuth2");
    CkJsonObjectW_UpdateString(entry,L"username",L"my-account");
    tokens = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(tokens,L"client_id",CkOAuth2W_clientId(oauth));
    CkJsonObjectW_UpdateString(tokens,L"redirect_uri",L"http://localhost:3017/");
    CkJsonObjectW_UpdateString(tokens,L"refresh_token",CkJsonObjectW_stringOf(reply,L"refresh_token"));
    now = CkDateTimeW_Create();
    CkDateTimeW_SetFromCurrentSystemTime(now);
    CkJsonObjectW_UpdateString(tokens,L"obtained_at_utc",CkDateTimeW_getAsTimestamp(now,FALSE));
    CkJsonObjectW_UpdateString(tokens,L"access_token",CkJsonObjectW_stringOf(reply,L"access_token"));
    CkJsonObjectW_UpdateInt(tokens,L"expires_in",CkJsonObjectW_IntOf(reply,L"expires_in"));
    saved = CkSecretsW_UpdateSecretJson(vault,entry,tokens);
    if (saved != TRUE) {
        wprintf(L"Could not save the current token record. Check the credential store.\n");
        CkOAuth2W_Dispose(oauth);
        CkJsonObjectW_Dispose(reply);
        CkSecretsW_Dispose(vault);
        CkJsonObjectW_Dispose(entry);
        CkJsonObjectW_Dispose(tokens);
        CkDateTimeW_Dispose(now);
        return;
    }

    wprintf(L"Authorization saved. Access-token lifetime in seconds: %d\n",CkJsonObjectW_IntOf(tokens,L"expires_in"));


    CkOAuth2W_Dispose(oauth);
    CkJsonObjectW_Dispose(reply);
    CkSecretsW_Dispose(vault);
    CkJsonObjectW_Dispose(entry);
    CkJsonObjectW_Dispose(tokens);
    CkDateTimeW_Dispose(now);

    }