Sample code for 30+ languages & platforms
Visual FoxPro Requires Chilkat v11.6.0+

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.

Microsoft documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loConfig
LOCAL loHttp
LOCAL loTokenEndpoint
LOCAL lcTokenUrl
LOCAL loCert
LOCAL loKey
LOCAL loDer
LOCAL loCrypt
LOCAL lcThumbprint
LOCAL loHeader
LOCAL loClaims
LOCAL lcAssertionId
LOCAL loClock
LOCAL loJwt
LOCAL lcAssertion
LOCAL loReq
LOCAL loResp
LOCAL loResult
LOCAL loEndpoint

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("outlook-calendar-app.json")
IF (lnSuccess <> 1) THEN
    ? "Could not load private app configuration."
    RELEASE loConfig
    CANCEL
ENDIF

IF ((loConfig.JsonTypeOf("tenant_id") <> 1) OR (loConfig.StringOfEquals("tenant_id","",1) = 1)) THEN
    ? "Expected a nonempty tenant_id in the JSON."
    RELEASE loConfig
    CANCEL
ENDIF

IF ((loConfig.JsonTypeOf("client_id") <> 1) OR (loConfig.StringOfEquals("client_id","",1) = 1)) THEN
    ? "Expected a nonempty client_id in the JSON."
    RELEASE loConfig
    CANCEL
ENDIF

IF ((loConfig.JsonTypeOf("mailbox") <> 1) OR (loConfig.StringOfEquals("mailbox","",1) = 1)) THEN
    ? "Expected a nonempty mailbox in the JSON."
    RELEASE loConfig
    CANCEL
ENDIF

IF ((loConfig.JsonTypeOf("pfx_path") <> 1) OR (loConfig.StringOfEquals("pfx_path","",1) = 1)) THEN
    ? "Expected a nonempty pfx_path in the JSON."
    RELEASE loConfig
    CANCEL
ENDIF

*  pfx_password may be empty for an unencrypted test PFX; protect the private key in production.
loHttp = CreateObject('Chilkat.Http')
loHttp.RequireSslCertVerify = 1
loHttp.FollowRedirects = 0
loHttp.ConnectTimeout = 20
loHttp.ReadTimeout = 30
loTokenEndpoint = CreateObject('Chilkat.StringBuilder')
loTokenEndpoint.Append("https://login.microsoftonline.com/")
loTokenEndpoint.Append(loHttp.UrlEncode(loConfig.StringOf("tenant_id")))
loTokenEndpoint.Append("/oauth2/v2.0/token")
lcTokenUrl = loTokenEndpoint.GetAsString()
loCert = CreateObject('Chilkat.Cert')
lnSuccess = loCert.LoadPfxFile(loConfig.StringOf("pfx_path"),loConfig.StringOf("pfx_password"))
IF (lnSuccess <> 1) THEN
    ? "Could not load the application certificate."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    CANCEL
ENDIF

loKey = CreateObject('Chilkat.PrivateKey')
lnSuccess = loCert.GetPrivateKey(loKey)
IF (lnSuccess <> 1) THEN
    ? "The certificate needs an accessible RSA private key."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    CANCEL
ENDIF

loDer = CreateObject('Chilkat.BinData')
lnSuccess = loCert.ExportCertDerBd(loDer)
IF (lnSuccess <> 1) THEN
    ? "Could not read the certificate DER bytes."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    CANCEL
ENDIF

loCrypt = CreateObject('Chilkat.Crypt2')
loCrypt.HashAlgorithm = "sha256"
loCrypt.EncodingMode = "base64url"
lcThumbprint = loCrypt.HashBdENC(loDer)
IF (loCrypt.LastMethodSuccess <> 1) THEN
    ? "Could not hash the certificate."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    CANCEL
ENDIF

loHeader = CreateObject('Chilkat.JsonObject')
loHeader.UpdateString("alg","PS256")
loHeader.UpdateString("typ","JWT")
loHeader.UpdateString("x5t#S256",lcThumbprint)
loClaims = CreateObject('Chilkat.JsonObject')
loClaims.UpdateString("aud",lcTokenUrl)
loClaims.UpdateString("iss",loConfig.StringOf("client_id"))
loClaims.UpdateString("sub",loConfig.StringOf("client_id"))
lcAssertionId = loCrypt.GenerateUuid()
IF (loCrypt.LastMethodSuccess <> 1) THEN
    ? "Could not create the assertion ID."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    CANCEL
ENDIF

loClaims.UpdateString("jti",lcAssertionId)
loClock = CreateObject('Chilkat.CkDateTime')
lnSuccess = loClock.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
    ? "Could not read the clock."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    CANCEL
ENDIF

*  Numeric dates are JSON numbers built from decimal strings, without converting timestamps to a platform-sized integer.
loClaims.UpdateNumber("iat",loClock.GetAsUnixTimeStr(0))
loClaims.UpdateNumber("nbf",loClock.GetAsUnixTimeStr(0))
lnSuccess = loClock.AddSeconds(300)
IF (lnSuccess <> 1) THEN
    ? "Could not set assertion expiry."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    CANCEL
ENDIF

loClaims.UpdateNumber("exp",loClock.GetAsUnixTimeStr(0))
loJwt = CreateObject('Chilkat.Jwt')
lcAssertion = loJwt.CreateJwtPk(loHeader.Emit(),loClaims.Emit(),loKey)
IF (loJwt.LastMethodSuccess <> 1) THEN
    ? "Could not sign the client assertion."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    CANCEL
ENDIF

loReq = CreateObject('Chilkat.HttpRequest')
loReq.HttpVerb = "POST"
loReq.ContentType = "application/x-www-form-urlencoded"
loReq.AddParam("client_id",loConfig.StringOf("client_id"))
loReq.AddParam("grant_type","client_credentials")
loReq.AddParam("scope","https://graph.microsoft.com/.default")
loReq.AddParam("client_assertion_type","urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
loReq.AddParam("client_assertion",lcAssertion)
loResp = CreateObject('Chilkat.HttpResponse')
loResult = CreateObject('Chilkat.JsonObject')
loResult.Strict = 1
lnSuccess = loHttp.HttpReq(lcTokenUrl,loReq,loResp)
*  A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
    ? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

IF (loResp.StatusCode <> 200) THEN
    ? "Unexpected HTTP status: " + STR(loResp.StatusCode)
    ? "Request ID: " + loResp.GetHeaderField("request-id")
    IF (loResp.StatusCode = 429) THEN
        ? "Retry-After: " + loResp.GetHeaderField("Retry-After")
    ENDIF

    IF (loResp.StatusCode = 401) THEN
        ? "Refresh the Graph access token or authorize again."
    ENDIF

    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a well-formed JSON response."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

IF ((loResult.JsonTypeOf("access_token") <> 1) OR (loResult.StringOfEquals("access_token","",1) = 1)) THEN
    ? "Expected a nonempty access_token in the JSON."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    CANCEL
ENDIF

loHttp.AuthToken = loResult.StringOf("access_token")
*  App-only tokens have no /me. Address the approved mailbox explicitly.
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("https://graph.microsoft.com/v1.0/users/")
loEndpoint.Append(loHttp.UrlEncode(loConfig.StringOf("mailbox")))
loEndpoint.Append("/calendar?$select=id,name,canEdit")
lnSuccess = loHttp.HttpNoBody("GET",loEndpoint.GetAsString(),loResp)
*  A false result can still include an HTTP error response.
IF ((lnSuccess <> 1) AND (loResp.StatusCode = 0)) THEN
    ? "No HTTP response was received. Inspect diagnostics privately; reconcile a write before retrying."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    RELEASE loEndpoint
    CANCEL
ENDIF

IF (loResp.StatusCode <> 200) THEN
    ? "Unexpected HTTP status: " + STR(loResp.StatusCode)
    ? "Request ID: " + loResp.GetHeaderField("request-id")
    IF (loResp.StatusCode = 429) THEN
        ? "Retry-After: " + loResp.GetHeaderField("Retry-After")
    ENDIF

    IF (loResp.StatusCode = 401) THEN
        ? "Refresh the Graph access token or authorize again."
    ENDIF

    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    RELEASE loEndpoint
    CANCEL
ENDIF

lnSuccess = loResult.Load(loResp.BodyStr)
IF (lnSuccess <> 1) THEN
    ? "Expected a well-formed JSON response."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    RELEASE loEndpoint
    CANCEL
ENDIF

IF ((loResult.JsonTypeOf("id") <> 1) OR (loResult.StringOfEquals("id","",1) = 1)) THEN
    ? "Expected a nonempty id in the JSON."
    RELEASE loConfig
    RELEASE loHttp
    RELEASE loTokenEndpoint
    RELEASE loCert
    RELEASE loKey
    RELEASE loDer
    RELEASE loCrypt
    RELEASE loHeader
    RELEASE loClaims
    RELEASE loClock
    RELEASE loJwt
    RELEASE loReq
    RELEASE loResp
    RELEASE loResult
    RELEASE loEndpoint
    CANCEL
ENDIF

? "Calendar ID: " + loResult.StringOf("id")
? "Calendar name: " + loResult.StringOf("name")

RELEASE loConfig
RELEASE loHttp
RELEASE loTokenEndpoint
RELEASE loCert
RELEASE loKey
RELEASE loDer
RELEASE loCrypt
RELEASE loHeader
RELEASE loClaims
RELEASE loClock
RELEASE loJwt
RELEASE loReq
RELEASE loResp
RELEASE loResult
RELEASE loEndpoint