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

Outlook Contacts: Access Microsoft 365 Contacts with a Certificate

See more Outlook Contact Examples

Authenticate an unattended application with an RSA certificate and read contacts in one explicitly named Microsoft 365 mailbox. Upload the public certificate to the Entra app registration, grant Graph application Contacts.Read permission and administrator consent, and restrict mailbox access using supported Exchange application-access controls. Use Contacts.ReadWrite only for operations that require writes. Personal Outlook.com accounts do not support this app-only flow.

Private outlook-contacts-app.json contains tenant_id (directory ID), client_id (Application ID), mailbox (object ID or UPN), pfx_path, and pfx_password. Use an organizational tenant rather than common or consumers. Protect the private key in an application environment you control.

A fresh five-minute PS256 assertion includes the SHA-256 certificate thumbprint in x5t#S256. The assertion authenticates only to the tenant token endpoint; the returned access token goes to Graph. The .default scope uses administrator-granted application permissions. There is no refresh token: obtain a new access token with a new assertion when needed.

Follow the complete @odata.nextLink and retain the immutable-ID preference. The code checks the host, rejects repeated continuations, and has a stated 100-page demonstration limit. Save a continuation privately to resume a larger scan. IDs are opaque strings, and names or email addresses are not unique identifiers.

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 contact 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
LOCAL lcUrl
LOCAL loNextLink
LOCAL loSeenLinks
LOCAL lnPageCount
LOCAL i
LOCAL lnNextType

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loConfig = CreateObject('Chilkat.JsonObject')
loConfig.Strict = 1
lnSuccess = loConfig.LoadFile("outlook-contacts-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")
*  Application tokens address an explicitly authorized mailbox, never /me.
loHttp.SetRequestHeader("Accept","application/json")
loHttp.SetRequestHeader("Prefer",'IdType="ImmutableId"')
loEndpoint = CreateObject('Chilkat.StringBuilder')
loEndpoint.Append("https://graph.microsoft.com/v1.0/users/")
loEndpoint.Append(loHttp.UrlEncode(loConfig.StringOf("mailbox")))
loEndpoint.Append("/contacts?$select=id,displayName&$top=50")
lcUrl = loEndpoint.GetAsString()
loNextLink = CreateObject('Chilkat.StringBuilder')
loSeenLinks = CreateObject('Chilkat.Hashtable')
lnPageCount = 0
DO WHILE (lnPageCount < 100)
    *  Use Microsoft's nextLink exactly as returned; do not rebuild skip tokens.
    loNextLink.Clear()
    loNextLink.Append(lcUrl)
    IF (loNextLink.StartsWith("https://graph.microsoft.com/v1.0/",1) <> 1) THEN
        ? "Unexpected pagination host or API version; no request sent."
        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
        RELEASE loNextLink
        RELEASE loSeenLinks
        CANCEL
    ENDIF

    IF (loSeenLinks.Contains(lcUrl) = 1) THEN
        ? "Repeated pagination URL; stopping."
        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
        RELEASE loNextLink
        RELEASE loSeenLinks
        CANCEL
    ENDIF

    loSeenLinks.AddInt(lcUrl,1)
    lnSuccess = loHttp.HttpNoBody("GET",lcUrl,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
        RELEASE loNextLink
        RELEASE loSeenLinks
        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
        RELEASE loNextLink
        RELEASE loSeenLinks
        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
        RELEASE loNextLink
        RELEASE loSeenLinks
        CANCEL
    ENDIF

    IF (loResult.JsonTypeOf("value") <> 4) THEN
        ? "Expected a value array."
        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
        RELEASE loNextLink
        RELEASE loSeenLinks
        CANCEL
    ENDIF

    i = 0
    DO WHILE (i < loResult.SizeOfArray("value"))
        loResult.I = i
        IF ((loResult.JsonTypeOf("value[i].id") <> 1) OR (loResult.StringOfEquals("value[i].id","",1) = 1)) THEN
            ? "Expected a nonempty value[i].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
            RELEASE loNextLink
            RELEASE loSeenLinks
            CANCEL
        ENDIF

        ? "Contact ID: " + loResult.StringOf("value[i].id")
        ? "Display name: " + loResult.StringOf("value[i].displayName")
        i = i + 1
    ENDDO
    lnPageCount = lnPageCount + 1
    *  Quote the member name because @odata.nextLink contains a literal period.
    lnNextType = loResult.JsonTypeOf('"@odata.nextLink"')
    IF ((lnNextType = -1) OR (lnNextType = 6)) THEN
        ? "Completed pages: " + STR(lnPageCount)
        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
        RELEASE loNextLink
        RELEASE loSeenLinks
        CANCEL
    ENDIF

    IF ((lnNextType <> 1) OR (loResult.StringOfEquals('"@odata.nextLink"',"",1) = 1)) THEN
        ? "Invalid continuation link; cannot establish completeness."
        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
        RELEASE loNextLink
        RELEASE loSeenLinks
        CANCEL
    ENDIF

    lcUrl = loResult.StringOf('"@odata.nextLink"')
ENDDO
? "Stopped at the example's 100-page limit. Save the continuation URL privately to resume."

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
RELEASE loNextLink
RELEASE loSeenLinks