Ruby Requires Chilkat v11.0.0+
Ruby
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 Ruby Downloads
require 'chilkat'
# Assumes Chilkat is unlocked. See Global Unlock Sample.
vault = Chilkat::CkSecrets.new()
vault.put_Location("local_manager")
vault.put_AutoSplit(true)
entry = Chilkat::CkJsonObject.new()
entry.UpdateString("appName","ConstantContactV3")
entry.UpdateString("service","OAuth2")
entry.UpdateString("username","my-account")
tokens = Chilkat::CkJsonObject.new()
loaded = vault.GetSecretJson(entry,tokens)
if (loaded != true)
print "Run the desktop authorization example using this account and store." + "\n";
exit
end
if ((tokens.JsonTypeOf("refresh_token") != 1) or (tokens.JsonTypeOf("client_id") != 1) or (tokens.JsonTypeOf("redirect_uri") != 1))
print "The saved PKCE grant is incomplete. Authorize again." + "\n";
exit
end
http = Chilkat::CkHttp.new()
http.put_RequireSslCertVerify(true)
form = Chilkat::CkHttpRequest.new()
form.put_HttpVerb("POST")
form.put_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"))
resp = Chilkat::CkHttpResponse.new()
sent = http.HttpReq("https://authz.constantcontact.com/oauth2/default/v1/token",form,resp)
reply = Chilkat::CkJsonObject.new()
parsed = reply.Load(resp.bodyStr())
if ((sent != true) or (resp.get_StatusCode() != 200))
print "Token refresh failed. HTTP status " + resp.get_StatusCode().to_s() + "\n";
if (reply.StringOfEquals("error","invalid_grant",true) == true)
print "The refresh grant is no longer valid. Authorize again." + "\n";
end
exit
end
if ((reply.JsonTypeOf("access_token") != 1) or (reply.IntOf("expires_in") <= 0))
print "Token response is missing an access token or lifetime." + "\n";
exit
end
if (reply.StringOfEquals("access_token","",true) == true)
print "The returned access token is empty." + "\n";
exit
end
if (reply.JsonTypeOf("refresh_token") == 1)
if (reply.StringOfEquals("refresh_token","",true) != true)
tokens.UpdateString("refresh_token",reply.stringOf("refresh_token"))
end
end
now = Chilkat::CkDateTime.new()
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"))
saved = vault.UpdateSecretJson(entry,tokens)
if (saved != true)
print "Could not save the current token record. Check the credential store." + "\n";
exit
end
print "Authorization saved. Access-token lifetime in seconds: " + tokens.IntOf("expires_in").to_s() + "\n";