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
#include <CkOAuth2W.h>
#include <CkJsonObjectW.h>
#include <CkSecretsW.h>
#include <CkDateTimeW.h>
void ChilkatSample(void)
{
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkOAuth2W oauth;
oauth.put_AuthorizationEndpoint(L"https://authz.constantcontact.com/oauth2/default/v1/authorize");
oauth.put_TokenEndpoint(L"https://authz.constantcontact.com/oauth2/default/v1/token");
oauth.put_ClientId(L"PKCE_CLIENT_ID");
oauth.put_LocalHost(L"localhost");
oauth.put_ListenPort(3017);
// Run authorization again if the account previously granted only contact access.
oauth.put_Scope(L"contact_data campaign_data offline_access");
oauth.put_CodeChallenge(true);
oauth.put_CodeChallengeMethod(L"S256");
oauth.put_UseBasicAuth(false);
const wchar_t *url = oauth.startAuth();
if (oauth.get_LastMethodSuccess() != true) {
wprintf(L"Could not start the local listener. Check the registered port.\n");
return;
}
bool opened = oauth.LaunchBrowser(url);
if (opened != true) {
oauth.Cancel();
wprintf(L"Could not open the system browser.\n");
return;
}
int waited = 0;
while (((waited < 300000) && (oauth.get_AuthFlowState() < 3))) {
oauth.SleepMs(100);
waited = waited + 100;
}
if (oauth.get_AuthFlowState() < 3) {
oauth.Cancel();
wprintf(L"Authorization timed out.\n");
return;
}
if (oauth.get_AuthFlowState() == 4) {
wprintf(L"Authorization was declined.\n");
return;
}
if (oauth.get_AuthFlowState() != 3) {
wprintf(L"Authorization failed. Check the PKCE app registration and exact redirect URI.\n");
return;
}
CkJsonObjectW reply;
bool parsed = reply.Load(oauth.accessTokenResponse());
if ((reply.JsonTypeOf(L"access_token") != 1) || (reply.IntOf(L"expires_in") <= 0)) {
wprintf(L"Token response is missing an access token or lifetime.\n");
return;
}
if (reply.StringOfEquals(L"access_token",L"",true) == true) {
wprintf(L"The returned access token is empty.\n");
return;
}
if ((reply.JsonTypeOf(L"refresh_token") != 1) || (reply.StringOfEquals(L"refresh_token",L"",true) == true)) {
wprintf(L"Missing refresh token. Check the offline_access scope and authorize again.\n");
return;
}
CkSecretsW vault;
vault.put_Location(L"local_manager");
vault.put_AutoSplit(true);
CkJsonObjectW entry;
entry.UpdateString(L"appName",L"ConstantContactV3");
entry.UpdateString(L"service",L"OAuth2");
entry.UpdateString(L"username",L"my-account");
CkJsonObjectW tokens;
tokens.UpdateString(L"client_id",oauth.clientId());
tokens.UpdateString(L"redirect_uri",L"http://localhost:3017/");
tokens.UpdateString(L"refresh_token",reply.stringOf(L"refresh_token"));
CkDateTimeW now;
now.SetFromCurrentSystemTime();
tokens.UpdateString(L"obtained_at_utc",now.getAsTimestamp(false));
tokens.UpdateString(L"access_token",reply.stringOf(L"access_token"));
tokens.UpdateInt(L"expires_in",reply.IntOf(L"expires_in"));
bool saved = vault.UpdateSecretJson(entry,tokens);
if (saved != true) {
wprintf(L"Could not save the current token record. Check the credential store.\n");
return;
}
wprintf(L"Authorization saved. Access-token lifetime in seconds: %d\n",tokens.IntOf(L"expires_in"));
}