Dart Requires Chilkat v11.0.0+
Dart
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 Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final vault = CkSecrets();
vault.location = 'local_manager';
vault.autoSplit = true;
final entry = CkJsonObject();
entry.updateString('appName', 'ConstantContactV3');
entry.updateString('service', 'OAuth2');
entry.updateString('username', 'my-account');
final tokens = CkJsonObject();
try {
vault.getSecretJson(entry, tokens);
} on ChilkatException {
print('Run the desktop authorization example using this account and store.');
return;
}
if ((tokens.jsonTypeOf('refresh_token') != 1) || ((tokens.jsonTypeOf('client_id') != 1) || (tokens.jsonTypeOf('redirect_uri') != 1))) {
print('The saved PKCE grant is incomplete. Authorize again.');
return;
}
final http = CkHttp();
http.requireSslCertVerify = true;
final form = CkHttpRequest();
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'));
final resp = CkHttpResponse();
var sent = true;
try {
http.httpReq('https://authz.constantcontact.com/oauth2/default/v1/token', form, resp);
} on ChilkatException {
sent = false;
}
final reply = CkJsonObject();
reply.load(resp.bodyStr);
if ((!sent) || (resp.statusCode != 200)) {
print('Token refresh failed. HTTP status ${resp.statusCode}');
if (reply.stringOfEquals('error', 'invalid_grant', true)) {
print('The refresh grant is no longer valid. Authorize again.');
}
return;
}
if ((reply.jsonTypeOf('access_token') != 1) || (reply.intOf('expires_in') <= 0)) {
print('Token response is missing an access token or lifetime.');
return;
}
if (reply.stringOfEquals('access_token', '', true)) {
print('The returned access token is empty.');
return;
}
if (reply.jsonTypeOf('refresh_token') == 1) {
if (!reply.stringOfEquals('refresh_token', '', true)) {
tokens.updateString('refresh_token', reply.stringOf('refresh_token'));
}
}
final now = 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'));
try {
vault.updateSecretJson(entry, tokens);
} on ChilkatException {
print('Could not save the current token record. Check the credential store.');
return;
}
print('Authorization saved. Access-token lifetime in seconds: ${tokens.intOf('expires_in')}');
}