PHP ActiveX Requires Chilkat v11.0.0+
PHP ActiveX
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 PHP ActiveX Downloads
<?php
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$vault = new COM("Chilkat.Secrets");
$vault->Location = 'local_manager';
$vault->AutoSplit = 1;
$entry = new COM("Chilkat.JsonObject");
$entry->UpdateString('appName','ConstantContactV3');
$entry->UpdateString('service','OAuth2');
$entry->UpdateString('username','my-account');
$tokens = new COM("Chilkat.JsonObject");
$loaded = $vault->GetSecretJson($entry,$tokens);
if ($loaded != 1) {
print 'Run the desktop authorization example using this account and store.' . "\n";
exit;
}
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;
}
$http = new COM("Chilkat.Http");
$http->RequireSslCertVerify = 1;
$form = new COM("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'));
$resp = new COM("Chilkat.HttpResponse");
$sent = $http->HttpReq('https://authz.constantcontact.com/oauth2/default/v1/token',$form,$resp);
$reply = new COM("Chilkat.JsonObject");
$parsed = $reply->Load($resp->BodyStr);
if (($sent != 1) or ($resp->StatusCode != 200)) {
print 'Token refresh failed. HTTP status ' . $resp->StatusCode . "\n";
if ($reply->StringOfEquals('error','invalid_grant',1) == 1) {
print 'The refresh grant is no longer valid. Authorize again.' . "\n";
}
exit;
}
if (($reply->JsonTypeOf('access_token') != 1) or ($reply->IntOf('expires_in') <= 0)) {
print 'Token response is missing an access token or lifetime.' . "\n";
exit;
}
if ($reply->StringOfEquals('access_token','',1) == 1) {
print 'The returned access token is empty.' . "\n";
exit;
}
if ($reply->JsonTypeOf('refresh_token') == 1) {
if ($reply->StringOfEquals('refresh_token','',1) != 1) {
$tokens->UpdateString('refresh_token',$reply->stringOf('refresh_token'));
}
}
$now = new COM("Chilkat.CkDateTime");
$now->SetFromCurrentSystemTime();
$tokens->UpdateString('obtained_at_utc',$now->getAsTimestamp(0));
$tokens->UpdateString('access_token',$reply->stringOf('access_token'));
$tokens->UpdateInt('expires_in',$reply->IntOf('expires_in'));
$saved = $vault->UpdateSecretJson($entry,$tokens);
if ($saved != 1) {
print 'Could not save the current token record. Check the credential store.' . "\n";
exit;
}
print 'Authorization saved. Access-token lifetime in seconds: ' . $tokens->IntOf('expires_in') . "\n";
?>