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

Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.OAuth2;
import com.chilkatsoft.chilkat2.Secrets;

public class ChilkatExample {

    public static void main(String[] args) {
        // Assumes Chilkat is unlocked. See Global Unlock Sample.
        OAuth2 oauth = new OAuth2();
        oauth.setAuthorizationEndpoint("https://authz.constantcontact.com/oauth2/default/v1/authorize");
        oauth.setTokenEndpoint("https://authz.constantcontact.com/oauth2/default/v1/token");
        oauth.setClientId("PKCE_CLIENT_ID");
        oauth.setLocalHost("localhost");
        oauth.setListenPort(3017);
        // Run authorization again if the account previously granted only contact access.
        oauth.setScope("contact_data campaign_data offline_access");
        oauth.setCodeChallenge(true);
        oauth.setCodeChallengeMethod("S256");
        oauth.setUseBasicAuth(false);
        String url;
        try {
            url = oauth.startAuth();
        } catch (ChilkatException e) {
            System.out.println("Could not start the local listener. Check the registered port.");
            return;
        }

        try {
            oauth.launchBrowser(url);
        } catch (ChilkatException e) {
            oauth.cancel();
            System.out.println("Could not open the system browser.");
            return;
        }

        int waited = 0;
        while ((waited < 300000) && (oauth.getAuthFlowState() < 3)) {
            oauth.sleepMs(100);
            waited = waited + 100;
        }

        if (oauth.getAuthFlowState() < 3) {
            oauth.cancel();
            System.out.println("Authorization timed out.");
            return;
        }

        if (oauth.getAuthFlowState() == 4) {
            System.out.println("Authorization was declined.");
            return;
        }

        if (oauth.getAuthFlowState() != 3) {
            System.out.println("Authorization failed. Check the PKCE app registration and exact redirect URI.");
            return;
        }

        JsonObject reply = new JsonObject();
        reply.load(oauth.getAccessTokenResponse());
        if ((reply.jsonTypeOf("access_token") != 1) || (reply.intOf("expires_in") <= 0)) {
            System.out.println("Token response is missing an access token or lifetime.");
            return;
        }

        if (reply.stringOfEquals("access_token", "", true)) {
            System.out.println("The returned access token is empty.");
            return;
        }

        if ((reply.jsonTypeOf("refresh_token") != 1) || (reply.stringOfEquals("refresh_token", "", true))) {
            System.out.println("Missing refresh token. Check the offline_access scope and authorize again.");
            return;
        }

        Secrets vault = new Secrets();
        vault.setLocation("local_manager");
        vault.setAutoSplit(true);
        JsonObject entry = new JsonObject();
        entry.updateString("appName", "ConstantContactV3");
        entry.updateString("service", "OAuth2");
        entry.updateString("username", "my-account");
        JsonObject tokens = new JsonObject();
        tokens.updateString("client_id", oauth.getClientId());
        tokens.updateString("redirect_uri", "http://localhost:3017/");
        tokens.updateString("refresh_token", reply.stringOf("refresh_token"));
        DateTime now = new DateTime();
        now.setFromCurrentSystemTime();
        tokens.updateString("obtained_at_utc", now.getAsTimestamp(false));
        tokens.updateString("access_token", reply.stringOf("access_token"));
        tokens.updateInt("expires_in", reply.intOf("expires_in"));
        try {
            vault.updateSecretJson(entry, tokens);
        } catch (ChilkatException e) {
            System.out.println("Could not save the current token record. Check the credential store.");
            return;
        }

        System.out.println("Authorization saved. Access-token lifetime in seconds: " + tokens.intOf("expires_in"));
    }
}