.NET Core C# Requires Chilkat v11.0.0+
.NET Core 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 .NET Core C# Downloads
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Chilkat.Secrets vault = new Chilkat.Secrets();
vault.Location = "local_manager";
vault.AutoSplit = true;
Chilkat.JsonObject entry = new Chilkat.JsonObject();
entry.UpdateString("appName","ConstantContactV3");
entry.UpdateString("service","OAuth2");
entry.UpdateString("username","my-account");
Chilkat.JsonObject tokens = new Chilkat.JsonObject();
bool loaded = vault.GetSecretJson(entry,tokens);
if (loaded != true) {
Debug.WriteLine("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)) {
Debug.WriteLine("The saved PKCE grant is incomplete. Authorize again.");
return;
}
Chilkat.Http http = new Chilkat.Http();
http.RequireSslCertVerify = true;
Chilkat.HttpRequest form = new Chilkat.HttpRequest();
form.HttpVerb = "POST";
form.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"));
Chilkat.HttpResponse resp = new Chilkat.HttpResponse();
bool sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp);
Chilkat.JsonObject reply = new Chilkat.JsonObject();
bool parsed = reply.Load(resp.BodyStr);
if ((sent != true) || (resp.StatusCode != 200)) {
Debug.WriteLine("Token refresh failed. HTTP status " + Convert.ToString(resp.StatusCode));
if (reply.StringOfEquals("error","invalid_grant",true) == true) {
Debug.WriteLine("The refresh grant is no longer valid. Authorize again.");
}
return;
}
if ((reply.JsonTypeOf("access_token") != 1) || (reply.IntOf("expires_in") <= 0)) {
Debug.WriteLine("Token response is missing an access token or lifetime.");
return;
}
if (reply.StringOfEquals("access_token","",true) == true) {
Debug.WriteLine("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"));
}
}
Chilkat.CkDateTime now = new Chilkat.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"));
bool saved = vault.UpdateSecretJson(entry,tokens);
if (saved != true) {
Debug.WriteLine("Could not save the current token record. Check the credential store.");
return;
}
Debug.WriteLine("Authorization saved. Access-token lifetime in seconds: " + Convert.ToString(tokens.IntOf("expires_in")));