DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Handle hoVault
Variant vEntry
Handle hoEntry
Boolean iSuccess
Variant vTokens
Handle hoTokens
Boolean iLoaded
Handle hoHttp
Variant vForm
Handle hoForm
Variant vResp
Handle hoResp
Boolean iSent
Handle hoReply
Boolean iParsed
Handle hoNow
Boolean iSaved
String sTemp1
Integer iTemp1
Integer iTemp2
Integer iTemp3
Boolean bTemp1
// Assumes Chilkat is unlocked. See Global Unlock Sample.
Get Create (RefClass(cComChilkatSecrets)) To hoVault
If (Not(IsComObjectCreated(hoVault))) Begin
Send CreateComObject of hoVault
End
Set ComLocation Of hoVault To "local_manager"
Set ComAutoSplit Of hoVault To True
Get Create (RefClass(cComChilkatJsonObject)) To hoEntry
If (Not(IsComObjectCreated(hoEntry))) Begin
Send CreateComObject of hoEntry
End
Get ComUpdateString Of hoEntry "appName" "ConstantContactV3" To iSuccess
Get ComUpdateString Of hoEntry "service" "OAuth2" To iSuccess
Get ComUpdateString Of hoEntry "username" "my-account" To iSuccess
Get Create (RefClass(cComChilkatJsonObject)) To hoTokens
If (Not(IsComObjectCreated(hoTokens))) Begin
Send CreateComObject of hoTokens
End
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoTokens to vTokens
Get ComGetSecretJson Of hoVault vEntry vTokens To iLoaded
If (iLoaded <> True) Begin
Showln "Run the desktop authorization example using this account and store."
Procedure_Return
End
Get ComJsonTypeOf Of hoTokens "refresh_token" To iTemp1
Get ComJsonTypeOf Of hoTokens "client_id" To iTemp2
Get ComJsonTypeOf Of hoTokens "redirect_uri" To iTemp3
If ((iTemp1 <> 1) Or (iTemp2 <> 1) Or (iTemp3 <> 1)) Begin
Showln "The saved PKCE grant is incomplete. Authorize again."
Procedure_Return
End
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Get Create (RefClass(cComChilkatHttpRequest)) To hoForm
If (Not(IsComObjectCreated(hoForm))) Begin
Send CreateComObject of hoForm
End
Set ComHttpVerb Of hoForm To "POST"
Set ComContentType Of hoForm To "application/x-www-form-urlencoded"
Send ComAddHeader To hoForm "Accept" "application/json"
Send ComAddParam To hoForm "grant_type" "refresh_token"
Get ComStringOf Of hoTokens "client_id" To sTemp1
Send ComAddParam To hoForm "client_id" sTemp1
Get ComStringOf Of hoTokens "redirect_uri" To sTemp1
Send ComAddParam To hoForm "redirect_uri" sTemp1
Get ComStringOf Of hoTokens "refresh_token" To sTemp1
Send ComAddParam To hoForm "refresh_token" sTemp1
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get pvComObject of hoForm to vForm
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp "https://authz.constantcontact.com/oauth2/default/v1/token" vForm vResp To iSent
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iParsed
Get ComStatusCode Of hoResp To iTemp1
If ((iSent <> True) Or (iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Token refresh failed. HTTP status " iTemp1
Get ComStringOfEquals Of hoReply "error" "invalid_grant" True To bTemp1
If (bTemp1 = True) Begin
Showln "The refresh grant is no longer valid. Authorize again."
End
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "access_token" To iTemp1
Get ComIntOf Of hoReply "expires_in" To iTemp2
If ((iTemp1 <> 1) Or (iTemp2 <= 0)) Begin
Showln "Token response is missing an access token or lifetime."
Procedure_Return
End
Get ComStringOfEquals Of hoReply "access_token" "" True To bTemp1
If (bTemp1 = True) Begin
Showln "The returned access token is empty."
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "refresh_token" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOfEquals Of hoReply "refresh_token" "" True To bTemp1
If (bTemp1 <> True) Begin
Get ComStringOf Of hoReply "refresh_token" To sTemp1
Get ComUpdateString Of hoTokens "refresh_token" sTemp1 To iSuccess
End
End
Get Create (RefClass(cComCkDateTime)) To hoNow
If (Not(IsComObjectCreated(hoNow))) Begin
Send CreateComObject of hoNow
End
Get ComSetFromCurrentSystemTime Of hoNow To iSuccess
Get ComGetAsTimestamp Of hoNow False To sTemp1
Get ComUpdateString Of hoTokens "obtained_at_utc" sTemp1 To iSuccess
Get ComStringOf Of hoReply "access_token" To sTemp1
Get ComUpdateString Of hoTokens "access_token" sTemp1 To iSuccess
Get ComIntOf Of hoReply "expires_in" To iTemp1
Get ComUpdateInt Of hoTokens "expires_in" iTemp1 To iSuccess
Get pvComObject of hoEntry to vEntry
Get pvComObject of hoTokens to vTokens
Get ComUpdateSecretJson Of hoVault vEntry vTokens To iSaved
If (iSaved <> True) Begin
Showln "Could not save the current token record. Check the credential store."
Procedure_Return
End
Get ComIntOf Of hoTokens "expires_in" To iTemp1
Showln "Authorization saved. Access-token lifetime in seconds: " iTemp1
End_Procedure