Sample code for 30+ languages & platforms
Visual Basic 6.0 Requires Chilkat v11.0.0+

Constant Contact V3: Authorize a desktop app with PKCE

See more Constant Contact Examples

Register a V3 app for the public-client PKCE flow and register http://localhost:3017/ exactly, including the trailing slash. Chilkat opens the system browser, receives the redirect on the same computer, and exchanges the code using the PKCE verifier. Request contact_data for contacts and lists, campaign_data for email campaigns and reporting, and offline_access for refresh. If the app already has a contacts-only grant, repeat this browser authorization to grant campaign access. Refreshing the old token cannot add a scope. Use a client ID from the PKCE app registration.

The examples share a Chilkat Secrets record. Windows Credential Manager and Apple Keychain persist the local record. On systems where the local manager uses process memory, configure a persistent Secrets backend for all examples. Give each authorized account its own record name. If migrating from the old token-file example, authorize once to populate the store.

Constant Contact developer guide

Chilkat Visual Basic 6.0 Downloads

Visual Basic 6.0
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
Dim oauth As New ChilkatOAuth2
oauth.AuthorizationEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/authorize"
oauth.TokenEndpoint = "https://authz.constantcontact.com/oauth2/default/v1/token"
oauth.ClientId = "PKCE_CLIENT_ID"
oauth.LocalHost = "localhost"
oauth.ListenPort = 3017
'  Run authorization again if the account previously granted only contact access.
oauth.Scope = "contact_data campaign_data offline_access"
oauth.CodeChallenge = 1
oauth.CodeChallengeMethod = "S256"
oauth.UseBasicAuth = 0
Dim url As String
url = oauth.StartAuth()
If (oauth.LastMethodSuccess <> 1) Then
    Debug.Print "Could not start the local listener. Check the registered port."
    Exit Sub
End If

Dim opened As Long
opened = oauth.LaunchBrowser(url)
If (opened <> 1) Then
    success = oauth.Cancel()
    Debug.Print "Could not open the system browser."
    Exit Sub
End If

Dim waited As Long
waited = 0
Do While ((waited < 300000) And (oauth.AuthFlowState < 3))
    oauth.SleepMs 100
    waited = waited + 100
Loop
If (oauth.AuthFlowState < 3) Then
    success = oauth.Cancel()
    Debug.Print "Authorization timed out."
    Exit Sub
End If

If (oauth.AuthFlowState = 4) Then
    Debug.Print "Authorization was declined."
    Exit Sub
End If

If (oauth.AuthFlowState <> 3) Then
    Debug.Print "Authorization failed. Check the PKCE app registration and exact redirect URI."
    Exit Sub
End If

Dim reply As New ChilkatJsonObject
Dim parsed As Long
parsed = reply.Load(oauth.AccessTokenResponse)
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) Or (reply.StringOfEquals("refresh_token","",1) = 1)) Then
    Debug.Print "Missing refresh token. Check the offline_access scope and authorize again."
    Exit Sub
End If

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
success = tokens.UpdateString("client_id",oauth.ClientId)
success = tokens.UpdateString("redirect_uri","http://localhost:3017/")
success = tokens.UpdateString("refresh_token",reply.StringOf("refresh_token"))
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")