Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
oleobject loo_Vault
oleobject loo_Entry
oleobject loo_Tokens
long li_Loaded
oleobject loo_Http
oleobject loo_Form
oleobject loo_Resp
long li_Sent
oleobject loo_Reply
long li_Parsed
oleobject loo_Now
long li_Saved

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Vault = create oleobject
li_rc = loo_Vault.ConnectToNewObject("Chilkat.Secrets")
if li_rc < 0 then
    destroy loo_Vault
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Vault.Location = "local_manager"
loo_Vault.AutoSplit = 1
loo_Entry = create oleobject
li_rc = loo_Entry.ConnectToNewObject("Chilkat.JsonObject")

loo_Entry.UpdateString("appName","ConstantContactV3")
loo_Entry.UpdateString("service","OAuth2")
loo_Entry.UpdateString("username","my-account")
loo_Tokens = create oleobject
li_rc = loo_Tokens.ConnectToNewObject("Chilkat.JsonObject")

li_Loaded = loo_Vault.GetSecretJson(loo_Entry,loo_Tokens)
if li_Loaded <> 1 then
    Write-Debug "Run the desktop authorization example using this account and store."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

if (loo_Tokens.JsonTypeOf("refresh_token") <> 1) OR (loo_Tokens.JsonTypeOf("client_id") <> 1) OR (loo_Tokens.JsonTypeOf("redirect_uri") <> 1) then
    Write-Debug "The saved PKCE grant is incomplete. Authorize again."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    return
end if

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Form = create oleobject
li_rc = loo_Form.ConnectToNewObject("Chilkat.HttpRequest")

loo_Form.HttpVerb = "POST"
loo_Form.ContentType = "application/x-www-form-urlencoded"
loo_Form.AddHeader("Accept","application/json")
loo_Form.AddParam("grant_type","refresh_token")
loo_Form.AddParam("client_id",loo_Tokens.StringOf("client_id"))
loo_Form.AddParam("redirect_uri",loo_Tokens.StringOf("redirect_uri"))
loo_Form.AddParam("refresh_token",loo_Tokens.StringOf("refresh_token"))
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

li_Sent = loo_Http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",loo_Form,loo_Resp)
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")

li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Sent <> 1) OR (loo_Resp.StatusCode <> 200) then
    Write-Debug "Token refresh failed. HTTP status " + string(loo_Resp.StatusCode)
    if loo_Reply.StringOfEquals("error","invalid_grant",1) = 1 then
        Write-Debug "The refresh grant is no longer valid. Authorize again."
    end if

    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if (loo_Reply.JsonTypeOf("access_token") <> 1) OR (loo_Reply.IntOf("expires_in") <= 0) then
    Write-Debug "Token response is missing an access token or lifetime."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.StringOfEquals("access_token","",1) = 1 then
    Write-Debug "The returned access token is empty."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.JsonTypeOf("refresh_token") = 1 then
    if loo_Reply.StringOfEquals("refresh_token","",1) <> 1 then
        loo_Tokens.UpdateString("refresh_token",loo_Reply.StringOf("refresh_token"))
    end if

end if

loo_Now = create oleobject
li_rc = loo_Now.ConnectToNewObject("Chilkat.CkDateTime")

loo_Now.SetFromCurrentSystemTime()
loo_Tokens.UpdateString("obtained_at_utc",loo_Now.GetAsTimestamp(0))
loo_Tokens.UpdateString("access_token",loo_Reply.StringOf("access_token"))
loo_Tokens.UpdateInt("expires_in",loo_Reply.IntOf("expires_in"))
li_Saved = loo_Vault.UpdateSecretJson(loo_Entry,loo_Tokens)
if li_Saved <> 1 then
    Write-Debug "Could not save the current token record. Check the credential store."
    destroy loo_Vault
    destroy loo_Entry
    destroy loo_Tokens
    destroy loo_Http
    destroy loo_Form
    destroy loo_Resp
    destroy loo_Reply
    destroy loo_Now
    return
end if

Write-Debug "Authorization saved. Access-token lifetime in seconds: " + string(loo_Tokens.IntOf("expires_in"))


destroy loo_Vault
destroy loo_Entry
destroy loo_Tokens
destroy loo_Http
destroy loo_Form
destroy loo_Resp
destroy loo_Reply
destroy loo_Now