Unicode C++ Requires Chilkat v11.0.0+
Unicode C++
Constant Contact V3: Refresh a PKCE access token
See more Constant Contact Examples
Run desktop authorization first. Refresh near the saved expiry or after a 401. Reuse the original client ID and redirect URI and save any rotated refresh token with the new access token. Serialize refreshes for the same account so two processes cannot overwrite the newest grant. If the grant is invalid, authorize again.
Constant Contact developer guide
Chilkat Unicode C++ Downloads
#include <CkSecretsW.h>
#include <CkJsonObjectW.h>
#include <CkHttpW.h>
#include <CkHttpRequestW.h>
#include <CkHttpResponseW.h>
#include <CkDateTimeW.h>
void ChilkatSample(void)
{
// Assumes Chilkat is unlocked. See Global Unlock Sample.
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;
bool loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
wprintf(L"Run the desktop authorization example using this account and store.\n");
return;
}
if ((tokens.JsonTypeOf(L"refresh_token") != 1) || (tokens.JsonTypeOf(L"client_id") != 1) || (tokens.JsonTypeOf(L"redirect_uri") != 1)) {
wprintf(L"The saved PKCE grant is incomplete. Authorize again.\n");
return;
}
CkHttpW http;
http.put_RequireSslCertVerify(true);
CkHttpRequestW form;
form.put_HttpVerb(L"POST");
form.put_ContentType(L"application/x-www-form-urlencoded");
form.AddHeader(L"Accept",L"application/json");
form.AddParam(L"grant_type",L"refresh_token");
form.AddParam(L"client_id",tokens.stringOf(L"client_id"));
form.AddParam(L"redirect_uri",tokens.stringOf(L"redirect_uri"));
form.AddParam(L"refresh_token",tokens.stringOf(L"refresh_token"));
CkHttpResponseW resp;
bool sent = http.HttpReq(L"https://authz.constantcontact.com/oauth2/default/v1/token",form,resp);
CkJsonObjectW reply;
bool parsed = reply.Load(resp.bodyStr());
if ((sent != true) || (resp.get_StatusCode() != 200)) {
wprintf(L"Token refresh failed. HTTP status %d\n",resp.get_StatusCode());
if (reply.StringOfEquals(L"error",L"invalid_grant",true) == true) {
wprintf(L"The refresh grant is no longer valid. Authorize again.\n");
}
return;
}
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) {
if (reply.StringOfEquals(L"refresh_token",L"",true) != true) {
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"));
}