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

Outlook Calendar: Authorize with OAuth2 and PKCE

See more Outlook Calendar Examples

Authorize an interactive desktop application using the Microsoft identity platform v2.0 authorization-code flow with PKCE (S256). This replaces the older public-client example that disabled PKCE, embedded a client secret, requested unrelated file/profile permissions, and printed tokens.

  1. Register an application in Microsoft Entra. Select personal Microsoft accounts, organizational accounts, or both according to the intended users. Copy the Application (client) ID, not the Object ID.
  2. Under Authentication, add a Mobile and desktop applications redirect URI of http://localhost:3017/. This is a local callback handled by Chilkat; it is not a hosted web-backend redirect.
  3. Add the delegated Microsoft Graph Calendars.ReadWrite permission for this example collection. A basic read-only application can instead use Calendars.ReadBasic; use Calendars.Read when fuller event information is required. Tenant consent policies can require an administrator even for delegated access.
  4. Use common only with the appropriate app account-type configuration, consumers for personal accounts only, organizations for organizational accounts, or your tenant ID for a single organization. Change both endpoints together.

offline_access requests a refresh token. Save the returned tokens privately and use the access token only for Graph. The saved obtained_at and expires_in describe its lifetime; refresh tokens can also expire or be revoked, so handle a need for interactive sign-in. This example saves a demonstration JSON cache; production applications should use protected credential storage and atomic replacement per account. No token is printed.

This system-browser/loopback example is intended for a machine with an interactive user. Web applications, mobile applications, and headless services need an authentication flow and redirect handling appropriate to their environment. Do not add a client secret to a public desktop application.

Microsoft documentation

Chilkat Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loOauth
LOCAL lcAuthUrl
LOCAL lnWaited
LOCAL loToken
LOCAL loObtained
LOCAL loFac

lnSuccess = 0

*  Assumes Chilkat is unlocked. See Global Unlock Sample.
lnSuccess = 0
loOauth = CreateObject('Chilkat.OAuth2')
*  Register a Mobile and desktop applications redirect URI: http://localhost:3017/
*  Use an account type that matches your app registration (common, consumers, or tenant ID).
loOauth.AuthorizationEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
loOauth.TokenEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
loOauth.ClientId = "YOUR_APPLICATION_CLIENT_ID"
*  Public desktop applications cannot keep a client secret. Use PKCE instead.
loOauth.LocalHost = "localhost"
loOauth.ListenPort = 3017
loOauth.CodeChallenge = 1
loOauth.CodeChallengeMethod = "S256"
*  ReadWrite covers this collection's create/delete operations. Use ReadBasic for basic lists only.
loOauth.Scope = "offline_access https://graph.microsoft.com/Calendars.ReadWrite"
lnSuccess = loOauth.AddAuthQueryParam("prompt","select_account")
IF (lnSuccess <> 1) THEN
    ? "Could not configure the authorization request."
    RELEASE loOauth
    CANCEL
ENDIF

lcAuthUrl = loOauth.StartAuth()
IF (loOauth.LastMethodSuccess <> 1) THEN
    ? "Could not start the local authorization listener."
    RELEASE loOauth
    CANCEL
ENDIF

lnSuccess = loOauth.LaunchBrowser(lcAuthUrl)
IF (lnSuccess <> 1) THEN
    loOauth.Cancel()
    ? "Could not open the system browser."
    RELEASE loOauth
    CANCEL
ENDIF

lnWaited = 0
DO WHILE ((loOauth.AuthFlowState < 3) AND (lnWaited < 180000))
    loOauth.SleepMs(200)
    lnWaited = lnWaited + 200
ENDDO
IF (loOauth.AuthFlowState < 3) THEN
    loOauth.Cancel()
    ? "Authorization timed out."
    RELEASE loOauth
    CANCEL
ENDIF

IF (loOauth.AuthFlowState <> 3) THEN
    ? "Authorization did not complete. Flow state: " + STR(loOauth.AuthFlowState)
    RELEASE loOauth
    CANCEL
ENDIF

loToken = CreateObject('Chilkat.JsonObject')
loToken.Strict = 1
lnSuccess = loToken.Load(loOauth.AccessTokenResponse)
IF (lnSuccess <> 1) THEN
    ? "Could not parse the token response; existing cache unchanged."
    RELEASE loOauth
    RELEASE loToken
    CANCEL
ENDIF

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

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

IF ((loToken.JsonTypeOf("expires_in") <> 2) OR (loToken.IntOf("expires_in") <= 0)) THEN
    ? "Expected a positive access-token lifetime."
    RELEASE loOauth
    RELEASE loToken
    CANCEL
ENDIF

loObtained = CreateObject('Chilkat.CkDateTime')
lnSuccess = loObtained.SetFromCurrentSystemTime()
IF (lnSuccess <> 1) THEN
    ? "Could not record the token acquisition time."
    RELEASE loOauth
    RELEASE loToken
    RELEASE loObtained
    CANCEL
ENDIF

loToken.UpdateString("obtained_at",loObtained.GetAsTimestamp(0))
*  This file contains credentials. Choose an access-controlled directory for the signed-in account.
loFac = CreateObject('Chilkat.FileAccess')
lnSuccess = loFac.DirEnsureExists("qa_data/tokens")
IF (lnSuccess <> 1) THEN
    ? "Could not create the private token-cache directory."
    RELEASE loOauth
    RELEASE loToken
    RELEASE loObtained
    RELEASE loFac
    CANCEL
ENDIF

lnSuccess = loToken.WriteFile("qa_data/tokens/outlookCalendar.json")
IF (lnSuccess <> 1) THEN
    ? "Could not save the token cache."
    RELEASE loOauth
    RELEASE loToken
    RELEASE loObtained
    RELEASE loFac
    CANCEL
ENDIF

*  Never print access_token, refresh_token, or the complete token response.
? "Calendar authorization completed; token cache saved."

RELEASE loOauth
RELEASE loToken
RELEASE loObtained
RELEASE loFac