Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL oVault
LOCAL oEntry
LOCAL oTokens
LOCAL nLoaded
LOCAL oHttp
LOCAL oForm
LOCAL oResp
LOCAL nSent
LOCAL oReply
LOCAL nParsed
LOCAL oNow
LOCAL nSaved
// Assumes Chilkat is unlocked. See Global Unlock Sample.
oVault := CreateObject("Chilkat.Secrets")
oVault:Location := "local_manager"
oVault:AutoSplit := 1
oEntry := CreateObject("Chilkat.JsonObject")
oEntry:UpdateString("appName", "ConstantContactV3")
oEntry:UpdateString("service", "OAuth2")
oEntry:UpdateString("username", "my-account")
oTokens := CreateObject("Chilkat.JsonObject")
nLoaded := oVault:GetSecretJson(oEntry, oTokens)
IF (nLoaded != 1)
? "Run the desktop authorization example using this account and store."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
RETURN
ENDIF
IF ((oTokens:JsonTypeOf("refresh_token") != 1) .OR. (oTokens:JsonTypeOf("client_id") != 1) .OR. (oTokens:JsonTypeOf("redirect_uri") != 1))
? "The saved PKCE grant is incomplete. Authorize again."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
RETURN
ENDIF
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oForm := CreateObject("Chilkat.HttpRequest")
oForm:HttpVerb := "POST"
oForm:ContentType := "application/x-www-form-urlencoded"
oForm:AddHeader("Accept", "application/json")
oForm:AddParam("grant_type", "refresh_token")
oForm:AddParam("client_id", oTokens:StringOf("client_id"))
oForm:AddParam("redirect_uri", oTokens:StringOf("redirect_uri"))
oForm:AddParam("refresh_token", oTokens:StringOf("refresh_token"))
oResp := CreateObject("Chilkat.HttpResponse")
nSent := oHttp:HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token", oForm, oResp)
oReply := CreateObject("Chilkat.JsonObject")
nParsed := oReply:Load(oResp:BodyStr)
IF ((nSent != 1) .OR. (oResp:StatusCode != 200))
? "Token refresh failed. HTTP status " + Str(oResp:StatusCode)
IF (oReply:StringOfEquals("error", "invalid_grant", 1) == 1)
? "The refresh grant is no longer valid. Authorize again."
ENDIF
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oForm:destroy()
oResp:destroy()
oReply:destroy()
RETURN
ENDIF
IF ((oReply:JsonTypeOf("access_token") != 1) .OR. (oReply:IntOf("expires_in") <= 0))
? "Token response is missing an access token or lifetime."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oForm:destroy()
oResp:destroy()
oReply:destroy()
RETURN
ENDIF
IF (oReply:StringOfEquals("access_token", "", 1) == 1)
? "The returned access token is empty."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oForm:destroy()
oResp:destroy()
oReply:destroy()
RETURN
ENDIF
IF (oReply:JsonTypeOf("refresh_token") == 1)
IF (oReply:StringOfEquals("refresh_token", "", 1) != 1)
oTokens:UpdateString("refresh_token", oReply:StringOf("refresh_token"))
ENDIF
ENDIF
oNow := CreateObject("Chilkat.CkDateTime")
oNow:SetFromCurrentSystemTime()
oTokens:UpdateString("obtained_at_utc", oNow:GetAsTimestamp(0))
oTokens:UpdateString("access_token", oReply:StringOf("access_token"))
oTokens:UpdateInt("expires_in", oReply:IntOf("expires_in"))
nSaved := oVault:UpdateSecretJson(oEntry, oTokens)
IF (nSaved != 1)
? "Could not save the current token record. Check the credential store."
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oForm:destroy()
oResp:destroy()
oReply:destroy()
oNow:destroy()
RETURN
ENDIF
? "Authorization saved. Access-token lifetime in seconds: " + Str(oTokens:IntOf("expires_in"))
oVault:destroy()
oEntry:destroy()
oTokens:destroy()
oHttp:destroy()
oForm:destroy()
oResp:destroy()
oReply:destroy()
oNow:destroy()