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