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

VBScript
Dim fso, outFile
Set fso = CreateObject("Scripting.FileSystemObject")
'Create a Unicode (utf-16) output text file.
Set outFile = fso.CreateTextFile("output.txt", True, True)

'  Assumes Chilkat is unlocked. See Global Unlock Sample.
set vault = CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
set tokens = CreateObject("Chilkat.JsonObject")
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
    outFile.WriteLine("Run the desktop authorization example using this account and store.")
    WScript.Quit
End If

If ((tokens.JsonTypeOf("refresh_token") <> 1) Or (tokens.JsonTypeOf("client_id") <> 1) Or (tokens.JsonTypeOf("redirect_uri") <> 1)) Then
    outFile.WriteLine("The saved PKCE grant is incomplete. Authorize again.")
    WScript.Quit
End If

set http = CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
set form = CreateObject("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")
set resp = CreateObject("Chilkat.HttpResponse")
sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp)
set reply = CreateObject("Chilkat.JsonObject")
parsed = reply.Load(resp.BodyStr)
If ((sent <> 1) Or (resp.StatusCode <> 200)) Then
    outFile.WriteLine("Token refresh failed. HTTP status " & resp.StatusCode)
    If (reply.StringOfEquals("error","invalid_grant",1) = 1) Then
        outFile.WriteLine("The refresh grant is no longer valid. Authorize again.")
    End If

    WScript.Quit
End If

If ((reply.JsonTypeOf("access_token") <> 1) Or (reply.IntOf("expires_in") <= 0)) Then
    outFile.WriteLine("Token response is missing an access token or lifetime.")
    WScript.Quit
End If

If (reply.StringOfEquals("access_token","",1) = 1) Then
    outFile.WriteLine("The returned access token is empty.")
    WScript.Quit
End If

If (reply.JsonTypeOf("refresh_token") = 1) Then
    If (reply.StringOfEquals("refresh_token","",1) <> 1) Then
        success = tokens.UpdateString("refresh_token",reply.StringOf("refresh_token"))
    End If

End If

set now = CreateObject("Chilkat.CkDateTime")
success = now.SetFromCurrentSystemTime()
success = tokens.UpdateString("obtained_at_utc",now.GetAsTimestamp(0))
success = tokens.UpdateString("access_token",reply.StringOf("access_token"))
success = tokens.UpdateInt("expires_in",reply.IntOf("expires_in"))
saved = vault.UpdateSecretJson(entry,tokens)
If (saved <> 1) Then
    outFile.WriteLine("Could not save the current token record. Check the credential store.")
    WScript.Quit
End If

outFile.WriteLine("Authorization saved. Access-token lifetime in seconds: " & tokens.IntOf("expires_in"))

outFile.Close