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

C
#include <C_CkOAuth2.h>
#include <C_CkJsonObject.h>
#include <C_CkSecrets.h>
#include <C_CkDateTime.h>

void ChilkatSample(void)
    {
    HCkOAuth2 oauth;
    const char *url;
    BOOL opened;
    int waited;
    HCkJsonObject reply;
    BOOL parsed;
    HCkSecrets vault;
    HCkJsonObject entry;
    HCkJsonObject tokens;
    HCkDateTime now;
    BOOL saved;

    //  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) {
        printf("Could not start the local listener. Check the registered port.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

    opened = CkOAuth2_LaunchBrowser(oauth,url);
    if (opened != TRUE) {
        CkOAuth2_Cancel(oauth);
        printf("Could not open the system browser.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

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

    if (CkOAuth2_getAuthFlowState(oauth) < 3) {
        CkOAuth2_Cancel(oauth);
        printf("Authorization timed out.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

    if (CkOAuth2_getAuthFlowState(oauth) == 4) {
        printf("Authorization was declined.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

    if (CkOAuth2_getAuthFlowState(oauth) != 3) {
        printf("Authorization failed. Check the PKCE app registration and exact redirect URI.\n");
        CkOAuth2_Dispose(oauth);
        return;
    }

    reply = CkJsonObject_Create();
    parsed = CkJsonObject_Load(reply,CkOAuth2_accessTokenResponse(oauth));
    if ((CkJsonObject_JsonTypeOf(reply,"access_token") != 1) || (CkJsonObject_IntOf(reply,"expires_in") <= 0)) {
        printf("Token response is missing an access token or lifetime.\n");
        CkOAuth2_Dispose(oauth);
        CkJsonObject_Dispose(reply);
        return;
    }

    if (CkJsonObject_StringOfEquals(reply,"access_token","",TRUE) == TRUE) {
        printf("The returned access token is empty.\n");
        CkOAuth2_Dispose(oauth);
        CkJsonObject_Dispose(reply);
        return;
    }

    if ((CkJsonObject_JsonTypeOf(reply,"refresh_token") != 1) || (CkJsonObject_StringOfEquals(reply,"refresh_token","",TRUE) == TRUE)) {
        printf("Missing refresh token. Check the offline_access scope and authorize again.\n");
        CkOAuth2_Dispose(oauth);
        CkJsonObject_Dispose(reply);
        return;
    }

    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) {
        printf("Could not save the current token record. Check the credential store.\n");
        CkOAuth2_Dispose(oauth);
        CkJsonObject_Dispose(reply);
        CkSecrets_Dispose(vault);
        CkJsonObject_Dispose(entry);
        CkJsonObject_Dispose(tokens);
        CkDateTime_Dispose(now);
        return;
    }

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


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

    }