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

Xojo Plugin
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As New Chilkat.Secrets
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As New Chilkat.JsonObject
Dim success As Boolean
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
Dim tokens As New Chilkat.JsonObject
Dim loaded As Boolean
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> True) Then
    System.DebugLog("Run the desktop authorization example using this account and store.")
    Return
End If

If ((tokens.JsonTypeOf("refresh_token") <> 1) Or (tokens.JsonTypeOf("client_id") <> 1) Or (tokens.JsonTypeOf("redirect_uri") <> 1)) Then
    System.DebugLog("The saved PKCE grant is incomplete. Authorize again.")
    Return
End If

Dim http As New Chilkat.Http
http.RequireSslCertVerify = True
Dim form As 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")
Dim resp As New Chilkat.HttpResponse
Dim sent As Boolean
sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp)
Dim reply As New Chilkat.JsonObject
Dim parsed As Boolean
parsed = reply.Load(resp.BodyStr)
If ((sent <> True) Or (resp.StatusCode <> 200)) Then
    System.DebugLog("Token refresh failed. HTTP status " + Str(resp.StatusCode))
    If (reply.StringOfEquals("error","invalid_grant",True) = True) Then
        System.DebugLog("The refresh grant is no longer valid. Authorize again.")
    End If

    Return
End If

If ((reply.JsonTypeOf("access_token") <> 1) Or (reply.IntOf("expires_in") <= 0)) Then
    System.DebugLog("Token response is missing an access token or lifetime.")
    Return
End If

If (reply.StringOfEquals("access_token","",True) = True) Then
    System.DebugLog("The returned access token is empty.")
    Return
End If

If (reply.JsonTypeOf("refresh_token") = 1) Then
    If (reply.StringOfEquals("refresh_token","",True) <> True) Then
        success = tokens.UpdateString("refresh_token",reply.StringOf("refresh_token"))
    End If

End If

Dim now As New Chilkat.CkDateTime
success = now.SetFromCurrentSystemTime()
success = tokens.UpdateString("obtained_at_utc",now.GetAsTimestamp(False))
success = tokens.UpdateString("access_token",reply.StringOf("access_token"))
success = tokens.UpdateInt("expires_in",reply.IntOf("expires_in"))
Dim saved As Boolean
saved = vault.UpdateSecretJson(entry,tokens)
If (saved <> True) Then
    System.DebugLog("Could not save the current token record. Check the credential store.")
    Return
End If

System.DebugLog("Authorization saved. Access-token lifetime in seconds: " + Str(tokens.IntOf("expires_in")))