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

VB.NET
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As New Chilkat.Secrets
vault.Location = "local_manager"
vault.AutoSplit = True
Dim entry As New Chilkat.JsonObject
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
Dim tokens As New Chilkat.JsonObject
Dim loaded As Boolean = vault.GetSecretJson(entry,tokens)
If (loaded <> True) Then
    Debug.WriteLine("Run the desktop authorization example using this account and store.")
    Exit Sub
End If

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

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

    Exit Sub
End If

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

If (reply.StringOfEquals("access_token","",True) = True) Then
    Debug.WriteLine("The returned access token is empty.")
    Exit Sub
End If

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

End If

Dim now As New 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"))
Dim saved As Boolean = vault.UpdateSecretJson(entry,tokens)
If (saved <> True) Then
    Debug.WriteLine("Could not save the current token record. Check the credential store.")
    Exit Sub
End If

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