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 CkJava Downloads
import com.chilkatsoft.*;
public class ChilkatExample {
static {
try {
System.loadLibrary("chilkat");
} catch (UnsatisfiedLinkError e) {
System.err.println("Native code library failed to load.\n" + e);
System.exit(1);
}
}
public static void main(String argv[])
{
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkOAuth2 oauth = new CkOAuth2();
oauth.put_AuthorizationEndpoint("https://authz.constantcontact.com/oauth2/default/v1/authorize");
oauth.put_TokenEndpoint("https://authz.constantcontact.com/oauth2/default/v1/token");
oauth.put_ClientId("PKCE_CLIENT_ID");
oauth.put_LocalHost("localhost");
oauth.put_ListenPort(3017);
// Run authorization again if the account previously granted only contact access.
oauth.put_Scope("contact_data campaign_data offline_access");
oauth.put_CodeChallenge(true);
oauth.put_CodeChallengeMethod("S256");
oauth.put_UseBasicAuth(false);
String url = oauth.startAuth();
if (oauth.get_LastMethodSuccess() != true) {
System.out.println("Could not start the local listener. Check the registered port.");
return;
}
boolean opened = oauth.LaunchBrowser(url);
if (opened != true) {
oauth.Cancel();
System.out.println("Could not open the system browser.");
return;
}
int waited = 0;
while (((waited < 300000) && (oauth.get_AuthFlowState() < 3))) {
oauth.SleepMs(100);
waited = waited+100;
}
if (oauth.get_AuthFlowState() < 3) {
oauth.Cancel();
System.out.println("Authorization timed out.");
return;
}
if (oauth.get_AuthFlowState() == 4) {
System.out.println("Authorization was declined.");
return;
}
if (oauth.get_AuthFlowState() != 3) {
System.out.println("Authorization failed. Check the PKCE app registration and exact redirect URI.");
return;
}
CkJsonObject reply = new CkJsonObject();
boolean parsed = reply.Load(oauth.accessTokenResponse());
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) == true) {
System.out.println("The returned access token is empty.");
return;
}
if ((reply.JsonTypeOf("refresh_token") != 1) || (reply.StringOfEquals("refresh_token","",true) == true)) {
System.out.println("Missing refresh token. Check the offline_access scope and authorize again.");
return;
}
CkSecrets vault = new CkSecrets();
vault.put_Location("local_manager");
vault.put_AutoSplit(true);
CkJsonObject entry = new CkJsonObject();
entry.UpdateString("appName","ConstantContactV3");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","my-account");
CkJsonObject tokens = new CkJsonObject();
tokens.UpdateString("client_id",oauth.clientId());
tokens.UpdateString("redirect_uri","http://localhost:3017/");
tokens.UpdateString("refresh_token",reply.stringOf("refresh_token"));
CkDateTime now = new CkDateTime();
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"));
boolean saved = vault.UpdateSecretJson(entry,tokens);
if (saved != true) {
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"));
}
}