AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$oVault = ObjCreate("Chilkat.Secrets")
$oVault.Location = "local_manager"
$oVault.AutoSplit = True
$oEntry = ObjCreate("Chilkat.JsonObject")
$oEntry.UpdateString("appName","ConstantContactV3")
$oEntry.UpdateString("service","OAuth2")
$oEntry.UpdateString("username","my-account")
$oTokens = ObjCreate("Chilkat.JsonObject")
Local $bLoaded = $oVault.GetSecretJson($oEntry,$oTokens)
If ($bLoaded <> True) Then
ConsoleWrite("Run the desktop authorization example using this account and store." & @CRLF)
Exit
EndIf
If (($oTokens.JsonTypeOf("refresh_token") <> 1) Or ($oTokens.JsonTypeOf("client_id") <> 1) Or ($oTokens.JsonTypeOf("redirect_uri") <> 1)) Then
ConsoleWrite("The saved PKCE grant is incomplete. Authorize again." & @CRLF)
Exit
EndIf
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oForm = ObjCreate("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 = ObjCreate("Chilkat.HttpResponse")
Local $bSent = $oHttp.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",$oForm,$oResp)
$oReply = ObjCreate("Chilkat.JsonObject")
Local $bParsed = $oReply.Load($oResp.BodyStr)
If (($bSent <> True) Or ($oResp.StatusCode <> 200)) Then
ConsoleWrite("Token refresh failed. HTTP status " & $oResp.StatusCode & @CRLF)
If ($oReply.StringOfEquals("error","invalid_grant",True) = True) Then
ConsoleWrite("The refresh grant is no longer valid. Authorize again." & @CRLF)
EndIf
Exit
EndIf
If (($oReply.JsonTypeOf("access_token") <> 1) Or ($oReply.IntOf("expires_in") <= 0)) Then
ConsoleWrite("Token response is missing an access token or lifetime." & @CRLF)
Exit
EndIf
If ($oReply.StringOfEquals("access_token","",True) = True) Then
ConsoleWrite("The returned access token is empty." & @CRLF)
Exit
EndIf
If ($oReply.JsonTypeOf("refresh_token") = 1) Then
If ($oReply.StringOfEquals("refresh_token","",True) <> True) Then
$oTokens.UpdateString("refresh_token",$oReply.StringOf("refresh_token"))
EndIf
EndIf
$oNow = ObjCreate("Chilkat.CkDateTime")
$oNow.SetFromCurrentSystemTime()
$oTokens.UpdateString("obtained_at_utc",$oNow.GetAsTimestamp(False))
$oTokens.UpdateString("access_token",$oReply.StringOf("access_token"))
$oTokens.UpdateInt("expires_in",$oReply.IntOf("expires_in"))
Local $bSaved = $oVault.UpdateSecretJson($oEntry,$oTokens)
If ($bSaved <> True) Then
ConsoleWrite("Could not save the current token record. Check the credential store." & @CRLF)
Exit
EndIf
ConsoleWrite("Authorization saved. Access-token lifetime in seconds: " & $oTokens.IntOf("expires_in") & @CRLF)