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

Visual FoxPro
LOCAL loVault
LOCAL loEntry
LOCAL loTokens
LOCAL lnLoaded
LOCAL loHttp
LOCAL loForm
LOCAL loResp
LOCAL lnSent
LOCAL loReply
LOCAL lnParsed
LOCAL loNow
LOCAL lnSaved

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
loVault = CreateObject('Chilkat.Secrets')
loVault.Location = "local_manager"
loVault.AutoSplit = 1
loEntry = CreateObject('Chilkat.JsonObject')
loEntry.UpdateString("appName","ConstantContactV3")
loEntry.UpdateString("service","OAuth2")
loEntry.UpdateString("username","my-account")
loTokens = CreateObject('Chilkat.JsonObject')
lnLoaded = loVault.GetSecretJson(loEntry,loTokens)
IF (lnLoaded <> 1) THEN
    ? "Run the desktop authorization example using this account and store."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

IF ((loTokens.JsonTypeOf("refresh_token") <> 1) OR (loTokens.JsonTypeOf("client_id") <> 1) OR (loTokens.JsonTypeOf("redirect_uri") <> 1)) THEN
    ? "The saved PKCE grant is incomplete. Authorize again."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    CANCEL
ENDIF

loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loForm = CreateObject('Chilkat.HttpRequest')
loForm.HttpVerb = "POST"
loForm.ContentType = "application/x-www-form-urlencoded"
loForm.AddHeader("Accept","application/json")
loForm.AddParam("grant_type","refresh_token")
loForm.AddParam("client_id",loTokens.StringOf("client_id"))
loForm.AddParam("redirect_uri",loTokens.StringOf("redirect_uri"))
loForm.AddParam("refresh_token",loTokens.StringOf("refresh_token"))
loResp = CreateObject('Chilkat.HttpResponse')
lnSent = loHttp.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",loForm,loResp)
loReply = CreateObject('Chilkat.JsonObject')
lnParsed = loReply.Load(loResp.BodyStr)
IF ((lnSent <> 1) OR (loResp.StatusCode <> 200)) THEN
    ? "Token refresh failed. HTTP status " + STR(loResp.StatusCode)
    IF (loReply.StringOfEquals("error","invalid_grant",1) = 1) THEN
        ? "The refresh grant is no longer valid. Authorize again."
    ENDIF

    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((loReply.JsonTypeOf("access_token") <> 1) OR (loReply.IntOf("expires_in") <= 0)) THEN
    ? "Token response is missing an access token or lifetime."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (loReply.StringOfEquals("access_token","",1) = 1) THEN
    ? "The returned access token is empty."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (loReply.JsonTypeOf("refresh_token") = 1) THEN
    IF (loReply.StringOfEquals("refresh_token","",1) <> 1) THEN
        loTokens.UpdateString("refresh_token",loReply.StringOf("refresh_token"))
    ENDIF

ENDIF

loNow = CreateObject('Chilkat.CkDateTime')
loNow.SetFromCurrentSystemTime()
loTokens.UpdateString("obtained_at_utc",loNow.GetAsTimestamp(0))
loTokens.UpdateString("access_token",loReply.StringOf("access_token"))
loTokens.UpdateInt("expires_in",loReply.IntOf("expires_in"))
lnSaved = loVault.UpdateSecretJson(loEntry,loTokens)
IF (lnSaved <> 1) THEN
    ? "Could not save the current token record. Check the credential store."
    RELEASE loVault
    RELEASE loEntry
    RELEASE loTokens
    RELEASE loHttp
    RELEASE loForm
    RELEASE loResp
    RELEASE loReply
    RELEASE loNow
    CANCEL
ENDIF

? "Authorization saved. Access-token lifetime in seconds: " + STR(loTokens.IntOf("expires_in"))

RELEASE loVault
RELEASE loEntry
RELEASE loTokens
RELEASE loHttp
RELEASE loForm
RELEASE loResp
RELEASE loReply
RELEASE loNow