Outlook Calendar: Access a Microsoft 365 Calendar with a Certificate
See more Outlook Calendar Examples
Obtain an app-only Graph token using a signed certificate assertion, then read an explicitly named Microsoft 365 mailbox's default calendar. Upload the public certificate to the Entra app registration and keep its RSA private key private. Grant the appropriate Graph application permission and administrator consent: Calendars.ReadBasic for this read, or a stronger permission only for the operations required. Scope mailbox access using your organization's supported Exchange application-access controls/RBAC; avoid unintended tenant-wide access.
Private outlook-calendar-app.json contains tenant_id (the directory ID), client_id (Application ID), mailbox (object ID or UPN), pfx_path, and pfx_password. Use an organization tenant, not common/consumers. This is not available for personal Outlook.com mailboxes. Certificate authentication is not a requirement to run a public web server, but the private key must remain in a trusted application environment.
The short-lived assertion uses PS256 and an x5t#S256 thumbprint of the certificate's DER bytes. It is a credential submitted only to the token endpoint, not the Graph bearer token. Client credentials returns no refresh token: obtain another access token with a fresh assertion when needed. The example never prints tokens or assertions.
HTTP methods can return false for an HTTP error as well as a connection failure. The code distinguishes those cases and checks the expected status before reading JSON. For 401, refresh the access token or authorize again; for 403, check permissions, mailbox access, and tenant policy. For 429, honor Retry-After before a bounded retry. Do not automatically repeat a write after an uncertain outcome. Keep detailed errors and calendar data private.
Chilkat PHP ActiveX Downloads
<?php
$success = 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = 0;
$config = new COM("Chilkat.JsonObject");
$config->Strict = 1;
$success = $config->LoadFile('outlook-calendar-app.json');
if ($success != 1) {
print 'Could not load private app configuration.' . "\n";
exit;
}
if (($config->JsonTypeOf('tenant_id') != 1) or ($config->StringOfEquals('tenant_id','',1) == 1)) {
print 'Expected a nonempty tenant_id in the JSON.' . "\n";
exit;
}
if (($config->JsonTypeOf('client_id') != 1) or ($config->StringOfEquals('client_id','',1) == 1)) {
print 'Expected a nonempty client_id in the JSON.' . "\n";
exit;
}
if (($config->JsonTypeOf('mailbox') != 1) or ($config->StringOfEquals('mailbox','',1) == 1)) {
print 'Expected a nonempty mailbox in the JSON.' . "\n";
exit;
}
if (($config->JsonTypeOf('pfx_path') != 1) or ($config->StringOfEquals('pfx_path','',1) == 1)) {
print 'Expected a nonempty pfx_path in the JSON.' . "\n";
exit;
}
// pfx_password may be empty for an unencrypted test PFX; protect the private key in production.
$http = new COM("Chilkat.Http");
$http->RequireSslCertVerify = 1;
$http->FollowRedirects = 0;
$http->ConnectTimeout = 20;
$http->ReadTimeout = 30;
$tokenEndpoint = new COM("Chilkat.StringBuilder");
$tokenEndpoint->Append('https://login.microsoftonline.com/');
$tokenEndpoint->Append($http->urlEncode($config->stringOf('tenant_id')));
$tokenEndpoint->Append('/oauth2/v2.0/token');
$tokenUrl = $tokenEndpoint->getAsString();
$cert = new COM("Chilkat.Cert");
$success = $cert->LoadPfxFile($config->stringOf('pfx_path'),$config->stringOf('pfx_password'));
if ($success != 1) {
print 'Could not load the application certificate.' . "\n";
exit;
}
$key = new COM("Chilkat.PrivateKey");
$success = $cert->GetPrivateKey($key);
if ($success != 1) {
print 'The certificate needs an accessible RSA private key.' . "\n";
exit;
}
$der = new COM("Chilkat.BinData");
$success = $cert->ExportCertDerBd($der);
if ($success != 1) {
print 'Could not read the certificate DER bytes.' . "\n";
exit;
}
$crypt = new COM("Chilkat.Crypt2");
$crypt->HashAlgorithm = 'sha256';
$crypt->EncodingMode = 'base64url';
$thumbprint = $crypt->hashBdENC($der);
if ($crypt->LastMethodSuccess != 1) {
print 'Could not hash the certificate.' . "\n";
exit;
}
$header = new COM("Chilkat.JsonObject");
$header->UpdateString('alg','PS256');
$header->UpdateString('typ','JWT');
$header->UpdateString('x5t#S256',$thumbprint);
$claims = new COM("Chilkat.JsonObject");
$claims->UpdateString('aud',$tokenUrl);
$claims->UpdateString('iss',$config->stringOf('client_id'));
$claims->UpdateString('sub',$config->stringOf('client_id'));
$assertionId = $crypt->generateUuid();
if ($crypt->LastMethodSuccess != 1) {
print 'Could not create the assertion ID.' . "\n";
exit;
}
$claims->UpdateString('jti',$assertionId);
$clock = new COM("Chilkat.CkDateTime");
$success = $clock->SetFromCurrentSystemTime();
if ($success != 1) {
print 'Could not read the clock.' . "\n";
exit;
}
// Numeric dates are JSON numbers built from decimal strings, without converting timestamps to a platform-sized integer.
$claims->UpdateNumber('iat',$clock->getAsUnixTimeStr(0));
$claims->UpdateNumber('nbf',$clock->getAsUnixTimeStr(0));
$success = $clock->AddSeconds(300);
if ($success != 1) {
print 'Could not set assertion expiry.' . "\n";
exit;
}
$claims->UpdateNumber('exp',$clock->getAsUnixTimeStr(0));
$jwt = new COM("Chilkat.Jwt");
$assertion = $jwt->createJwtPk($header->emit(),$claims->emit(),$key);
if ($jwt->LastMethodSuccess != 1) {
print 'Could not sign the client assertion.' . "\n";
exit;
}
$req = new COM("Chilkat.HttpRequest");
$req->HttpVerb = 'POST';
$req->ContentType = 'application/x-www-form-urlencoded';
$req->AddParam('client_id',$config->stringOf('client_id'));
$req->AddParam('grant_type','client_credentials');
$req->AddParam('scope','https://graph.microsoft.com/.default');
$req->AddParam('client_assertion_type','urn:ietf:params:oauth:client-assertion-type:jwt-bearer');
$req->AddParam('client_assertion',$assertion);
$resp = new COM("Chilkat.HttpResponse");
$result = new COM("Chilkat.JsonObject");
$result->Strict = 1;
$success = $http->HttpReq($tokenUrl,$req,$resp);
// A false result can still include an HTTP error response.
if (($success != 1) and ($resp->StatusCode == 0)) {
print 'No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.' . "\n";
exit;
}
if ($resp->StatusCode != 200) {
print 'Unexpected HTTP status: ' . $resp->StatusCode . "\n";
print 'Request ID: ' . $resp->getHeaderField('request-id') . "\n";
if ($resp->StatusCode == 429) {
print 'Retry-After: ' . $resp->getHeaderField('Retry-After') . "\n";
}
if ($resp->StatusCode == 401) {
print 'Refresh the Graph access token or authorize again.' . "\n";
}
exit;
}
$success = $result->Load($resp->BodyStr);
if ($success != 1) {
print 'Expected a well-formed JSON response.' . "\n";
exit;
}
if (($result->JsonTypeOf('access_token') != 1) or ($result->StringOfEquals('access_token','',1) == 1)) {
print 'Expected a nonempty access_token in the JSON.' . "\n";
exit;
}
$http->AuthToken = $result->stringOf('access_token');
// App-only tokens have no /me. Address the approved mailbox explicitly.
$endpoint = new COM("Chilkat.StringBuilder");
$endpoint->Append('https://graph.microsoft.com/v1.0/users/');
$endpoint->Append($http->urlEncode($config->stringOf('mailbox')));
$endpoint->Append('/calendar?$select=id,name,canEdit');
$success = $http->HttpNoBody('GET',$endpoint->getAsString(),$resp);
// A false result can still include an HTTP error response.
if (($success != 1) and ($resp->StatusCode == 0)) {
print 'No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying.' . "\n";
exit;
}
if ($resp->StatusCode != 200) {
print 'Unexpected HTTP status: ' . $resp->StatusCode . "\n";
print 'Request ID: ' . $resp->getHeaderField('request-id') . "\n";
if ($resp->StatusCode == 429) {
print 'Retry-After: ' . $resp->getHeaderField('Retry-After') . "\n";
}
if ($resp->StatusCode == 401) {
print 'Refresh the Graph access token or authorize again.' . "\n";
}
exit;
}
$success = $result->Load($resp->BodyStr);
if ($success != 1) {
print 'Expected a well-formed JSON response.' . "\n";
exit;
}
if (($result->JsonTypeOf('id') != 1) or ($result->StringOfEquals('id','',1) == 1)) {
print 'Expected a nonempty id in the JSON.' . "\n";
exit;
}
print 'Calendar ID: ' . $result->stringOf('id') . "\n";
print 'Calendar name: ' . $result->stringOf('name') . "\n";
?>