Visual Basic 6.0 Requires Chilkat v11.0.0+
Visual Basic 6.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 Visual Basic 6.0 Downloads
' Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim vault As New ChilkatSecrets
vault.Location = "local_manager"
vault.AutoSplit = 1
Dim entry As New ChilkatJsonObject
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
Dim tokens As New ChilkatJsonObject
Dim loaded As Long
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
Debug.Print "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.Print "The saved PKCE grant is incomplete. Authorize again."
Exit Sub
End If
Dim http As New ChilkatHttp
http.RequireSslCertVerify = 1
Dim form As New ChilkatHttpRequest
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 ChilkatHttpResponse
Dim sent As Long
sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp)
Dim reply As New ChilkatJsonObject
Dim parsed As Long
parsed = reply.Load(resp.BodyStr)
If ((sent <> 1) Or (resp.StatusCode <> 200)) Then
Debug.Print "Token refresh failed. HTTP status " & resp.StatusCode
If (reply.StringOfEquals("error","invalid_grant",1) = 1) Then
Debug.Print "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.Print "Token response is missing an access token or lifetime."
Exit Sub
End If
If (reply.StringOfEquals("access_token","",1) = 1) Then
Debug.Print "The returned access token is empty."
Exit Sub
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
Dim now As New 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"))
Dim saved As Long
saved = vault.UpdateSecretJson(entry,tokens)
If (saved <> 1) Then
Debug.Print "Could not save the current token record. Check the credential store."
Exit Sub
End If
Debug.Print "Authorization saved. Access-token lifetime in seconds: " & tokens.IntOf("expires_in")