Classic ASP Requires Chilkat v11.0.0+
Classic ASP
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 Classic ASP Downloads
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
' Assumes Chilkat is unlocked. See Global Unlock Sample.
set vault = Server.CreateObject("Chilkat.Secrets")
vault.Location = "local_manager"
vault.AutoSplit = 1
set entry = Server.CreateObject("Chilkat.JsonObject")
success = entry.UpdateString("appName","ConstantContactV3")
success = entry.UpdateString("service","OAuth2")
success = entry.UpdateString("username","my-account")
set tokens = Server.CreateObject("Chilkat.JsonObject")
loaded = vault.GetSecretJson(entry,tokens)
If (loaded <> 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "Run the desktop authorization example using this account and store.") & "</pre>"
Response.End
End If
If ((tokens.JsonTypeOf("refresh_token") <> 1) Or (tokens.JsonTypeOf("client_id") <> 1) Or (tokens.JsonTypeOf("redirect_uri") <> 1)) Then
Response.Write "<pre>" & Server.HTMLEncode( "The saved PKCE grant is incomplete. Authorize again.") & "</pre>"
Response.End
End If
set http = Server.CreateObject("Chilkat.Http")
http.RequireSslCertVerify = 1
set form = Server.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 = Server.CreateObject("Chilkat.HttpResponse")
sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp)
set reply = Server.CreateObject("Chilkat.JsonObject")
parsed = reply.Load(resp.BodyStr)
If ((sent <> 1) Or (resp.StatusCode <> 200)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Token refresh failed. HTTP status " & resp.StatusCode) & "</pre>"
If (reply.StringOfEquals("error","invalid_grant",1) = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "The refresh grant is no longer valid. Authorize again.") & "</pre>"
End If
Response.End
End If
If ((reply.JsonTypeOf("access_token") <> 1) Or (reply.IntOf("expires_in") <= 0)) Then
Response.Write "<pre>" & Server.HTMLEncode( "Token response is missing an access token or lifetime.") & "</pre>"
Response.End
End If
If (reply.StringOfEquals("access_token","",1) = 1) Then
Response.Write "<pre>" & Server.HTMLEncode( "The returned access token is empty.") & "</pre>"
Response.End
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 = Server.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
Response.Write "<pre>" & Server.HTMLEncode( "Could not save the current token record. Check the credential store.") & "</pre>"
Response.End
End If
Response.Write "<pre>" & Server.HTMLEncode( "Authorization saved. Access-token lifetime in seconds: " & tokens.IntOf("expires_in")) & "</pre>"
%>
</body>
</html>