C++ Requires Chilkat v11.0.0+
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 C++ Downloads
#include <CkSecrets.h>
#include <CkJsonObject.h>
#include <CkHttp.h>
#include <CkHttpRequest.h>
#include <CkHttpResponse.h>
#include <CkDateTime.h>
void ChilkatSample(void)
{
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkSecrets vault;
vault.put_Location("local_manager");
vault.put_AutoSplit(true);
CkJsonObject entry;
entry.UpdateString("appName","ConstantContactV3");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","my-account");
CkJsonObject tokens;
bool loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
std::cout << "Run the desktop authorization example using this account and store." << "\r\n";
return;
}
if ((tokens.JsonTypeOf("refresh_token") != 1) || (tokens.JsonTypeOf("client_id") != 1) || (tokens.JsonTypeOf("redirect_uri") != 1)) {
std::cout << "The saved PKCE grant is incomplete. Authorize again." << "\r\n";
return;
}
CkHttp http;
http.put_RequireSslCertVerify(true);
CkHttpRequest form;
form.put_HttpVerb("POST");
form.put_ContentType("application/x-www-form-urlencoded");
form.AddHeader("Accept","application/json");
form.AddParam("grant_type","refresh_token");
form.AddParam("client_id",tokens.stringOf("client_id"));
form.AddParam("redirect_uri",tokens.stringOf("redirect_uri"));
form.AddParam("refresh_token",tokens.stringOf("refresh_token"));
CkHttpResponse resp;
bool sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp);
CkJsonObject reply;
bool parsed = reply.Load(resp.bodyStr());
if ((sent != true) || (resp.get_StatusCode() != 200)) {
std::cout << "Token refresh failed. HTTP status " << resp.get_StatusCode() << "\r\n";
if (reply.StringOfEquals("error","invalid_grant",true) == true) {
std::cout << "The refresh grant is no longer valid. Authorize again." << "\r\n";
}
return;
}
if ((reply.JsonTypeOf("access_token") != 1) || (reply.IntOf("expires_in") <= 0)) {
std::cout << "Token response is missing an access token or lifetime." << "\r\n";
return;
}
if (reply.StringOfEquals("access_token","",true) == true) {
std::cout << "The returned access token is empty." << "\r\n";
return;
}
if (reply.JsonTypeOf("refresh_token") == 1) {
if (reply.StringOfEquals("refresh_token","",true) != true) {
tokens.UpdateString("refresh_token",reply.stringOf("refresh_token"));
}
}
CkDateTime now;
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"));
bool saved = vault.UpdateSecretJson(entry,tokens);
if (saved != true) {
std::cout << "Could not save the current token record. Check the credential store." << "\r\n";
return;
}
std::cout << "Authorization saved. Access-token lifetime in seconds: " << tokens.IntOf("expires_in") << "\r\n";
}