Java Requires Chilkat v11.0.0+
Java
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 Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.DateTime;
import com.chilkatsoft.chilkat2.Http;
import com.chilkatsoft.chilkat2.HttpRequest;
import com.chilkatsoft.chilkat2.HttpResponse;
import com.chilkatsoft.chilkat2.JsonObject;
import com.chilkatsoft.chilkat2.Secrets;
public class ChilkatExample {
public static void main(String[] args) {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Secrets vault = new Secrets();
vault.setLocation("local_manager");
vault.setAutoSplit(true);
JsonObject entry = new JsonObject();
entry.updateString("appName", "ConstantContactV3");
entry.updateString("service", "OAuth2");
entry.updateString("username", "my-account");
JsonObject tokens = new JsonObject();
try {
vault.getSecretJson(entry, tokens);
} catch (ChilkatException e) {
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;
}
Http http = new Http();
http.setRequireSslCertVerify(true);
HttpRequest form = new HttpRequest();
form.setHttpVerb("POST");
form.setContentType("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"));
HttpResponse resp = new HttpResponse();
boolean sent;
try {
http.httpReq("https://authz.constantcontact.com/oauth2/default/v1/token", form, resp);
sent = true;
} catch (ChilkatException e) {
sent = false;
}
JsonObject reply = new JsonObject();
reply.load(resp.getBodyStr());
if ((!sent) || (resp.getStatusCode() != 200)) {
System.out.println("Token refresh failed. HTTP status " + resp.getStatusCode());
if (reply.stringOfEquals("error", "invalid_grant", 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)) {
System.out.println("The returned access token is empty.");
return;
}
if (reply.jsonTypeOf("refresh_token") == 1) {
if (!reply.stringOfEquals("refresh_token", "", true)) {
tokens.updateString("refresh_token", reply.stringOf("refresh_token"));
}
}
DateTime now = new DateTime();
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"));
try {
vault.updateSecretJson(entry, tokens);
} catch (ChilkatException e) {
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"));
}
}