Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local vault = chilkat.Secrets.new()
vault.location = "local_manager"
vault.auto_split = true
local entry = chilkat.JsonObject.new()
entry:update_string("appName", "ConstantContactV3")
entry:update_string("service", "OAuth2")
entry:update_string("username", "my-account")
local tokens = chilkat.JsonObject.new()
local loaded = vault:get_secret_json(entry, tokens)
if not loaded then
    print("Run the desktop authorization example using this account and store.")
    return
end

if (tokens:json_type_of("refresh_token") ~= 1) or ((tokens:json_type_of("client_id") ~= 1) or (tokens:json_type_of("redirect_uri") ~= 1)) then
    print("The saved PKCE grant is incomplete. Authorize again.")
    return
end

local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
local form = chilkat.HttpRequest.new()
form.http_verb = "POST"
form.content_type = "application/x-www-form-urlencoded"
form:add_header("Accept", "application/json")
form:add_param("grant_type", "refresh_token")
form:add_param("client_id", tokens:string_of("client_id"))
form:add_param("redirect_uri", tokens:string_of("redirect_uri"))
form:add_param("refresh_token", tokens:string_of("refresh_token"))
local resp = chilkat.HttpResponse.new()
local sent = http:http_req("https://authz.constantcontact.com/oauth2/default/v1/token", form, resp)
local reply = chilkat.JsonObject.new()
local parsed = reply:load(resp.body_str)
if (not sent) or (resp.status_code ~= 200) then
    print("Token refresh failed. HTTP status " .. resp.status_code)
    if reply:string_of_equals("error", "invalid_grant", true) then
        print("The refresh grant is no longer valid. Authorize again.")
    end

    return
end

if (reply:json_type_of("access_token") ~= 1) or (reply:int_of("expires_in") <= 0) then
    print("Token response is missing an access token or lifetime.")
    return
end

if reply:string_of_equals("access_token", "", true) then
    print("The returned access token is empty.")
    return
end

if reply:json_type_of("refresh_token") == 1 then
    if not reply:string_of_equals("refresh_token", "", true) then
        tokens:update_string("refresh_token", reply:string_of("refresh_token"))
    end
end

local now = chilkat.DateTime.new()
now:set_from_current_system_time()
tokens:update_string("obtained_at_utc", now:get_as_timestamp(false))
tokens:update_string("access_token", reply:string_of("access_token"))
tokens:update_int("expires_in", reply:int_of("expires_in"))
local saved = vault:update_secret_json(entry, tokens)
if not saved then
    print("Could not save the current token record. Check the credential store.")
    return
end

print("Authorization saved. Access-token lifetime in seconds: " .. tokens:int_of("expires_in"))