CkJava Requires Chilkat v11.0.0+
CkJava
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 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.
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();
boolean loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
System.out.println("Run the desktop authorization example using this account and store.");
return;
}
if ((tokens.JsonTypeOf("refresh_token") != 1) || (tokens.JsonTypeOf("client_id") != 1) || (tokens.JsonTypeOf("redirect_uri") != 1)) {
System.out.println("The saved PKCE grant is incomplete. Authorize again.");
return;
}
CkHttp http = new CkHttp();
http.put_RequireSslCertVerify(true);
CkHttpRequest form = new CkHttpRequest();
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 = new CkHttpResponse();
boolean sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp);
CkJsonObject reply = new CkJsonObject();
boolean parsed = reply.Load(resp.bodyStr());
if ((sent != true) || (resp.get_StatusCode() != 200)) {
System.out.println("Token refresh failed. HTTP status " + resp.get_StatusCode());
if (reply.StringOfEquals("error","invalid_grant",true) == true) {
System.out.println("The refresh grant is no longer valid. Authorize again.");
}
return;
}
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) {
if (reply.StringOfEquals("refresh_token","",true) != true) {
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"));
}
}