PowerShell Requires Chilkat v11.0.0+
PowerShell
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 PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = New-Object Chilkat.Secrets
$vault.Location = "local_manager"
$vault.AutoSplit = $true
$entry = New-Object Chilkat.JsonObject
$entry.UpdateString("appName","ConstantContactV3")
$entry.UpdateString("service","OAuth2")
$entry.UpdateString("username","my-account")
$tokens = New-Object Chilkat.JsonObject
$loaded = $vault.GetSecretJson($entry,$tokens)
if ($loaded -ne $true) {
$("Run the desktop authorization example using this account and store.")
exit
}
if (($tokens.JsonTypeOf("refresh_token") -ne 1) -or ($tokens.JsonTypeOf("client_id") -ne 1) -or ($tokens.JsonTypeOf("redirect_uri") -ne 1)) {
$("The saved PKCE grant is incomplete. Authorize again.")
exit
}
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$form = New-Object 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"))
$resp = New-Object Chilkat.HttpResponse
$sent = $http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",$form,$resp)
$reply = New-Object Chilkat.JsonObject
$parsed = $reply.Load($resp.BodyStr)
if (($sent -ne $true) -or ($resp.StatusCode -ne 200)) {
$("Token refresh failed. HTTP status " + $resp.StatusCode)
if ($reply.StringOfEquals("error","invalid_grant",$true) -eq $true) {
$("The refresh grant is no longer valid. Authorize again.")
}
exit
}
if (($reply.JsonTypeOf("access_token") -ne 1) -or ($reply.IntOf("expires_in") -le 0)) {
$("Token response is missing an access token or lifetime.")
exit
}
if ($reply.StringOfEquals("access_token","",$true) -eq $true) {
$("The returned access token is empty.")
exit
}
if ($reply.JsonTypeOf("refresh_token") -eq 1) {
if ($reply.StringOfEquals("refresh_token","",$true) -ne $true) {
$tokens.UpdateString("refresh_token",$reply.StringOf("refresh_token"))
}
}
$now = New-Object Chilkat.CkDateTime
$now.SetFromCurrentSystemTime()
$tokens.UpdateString("obtained_at_utc",$now.GetAsTimestamp($false))
$tokens.UpdateString("access_token",$reply.StringOf("access_token"))
$tokens.UpdateInt("expires_in",$reply.IntOf("expires_in"))
$saved = $vault.UpdateSecretJson($entry,$tokens)
if ($saved -ne $true) {
$("Could not save the current token record. Check the credential store.")
exit
}
$("Authorization saved. Access-token lifetime in seconds: " + $tokens.IntOf("expires_in"))