Sample code for 30+ languages & platforms
PowerBuilder 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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Oauth
string ls_AuthUrl
long li_Waited
oleobject loo_Token
oleobject loo_Obtained
oleobject loo_Fac

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
li_Success = 0
loo_Oauth = create oleobject
li_rc = loo_Oauth.ConnectToNewObject("Chilkat.OAuth2")
if li_rc < 0 then
    destroy loo_Oauth
    MessageBox("Error","Connecting to COM object failed")
    return
end if
//  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).
loo_Oauth.AuthorizationEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
loo_Oauth.TokenEndpoint = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
loo_Oauth.ClientId = "YOUR_APPLICATION_CLIENT_ID"
//  Public desktop applications cannot keep a client secret. Use PKCE instead.
loo_Oauth.LocalHost = "localhost"
loo_Oauth.ListenPort = 3017
loo_Oauth.CodeChallenge = 1
loo_Oauth.CodeChallengeMethod = "S256"
//  ReadWrite covers this collection's create/delete operations. Use ReadBasic for basic lists only.
loo_Oauth.Scope = "offline_access https://graph.microsoft.com/Calendars.ReadWrite"
li_Success = loo_Oauth.AddAuthQueryParam("prompt","select_account")
if li_Success <> 1 then
    Write-Debug "Could not configure the authorization request."
    destroy loo_Oauth
    return
end if

ls_AuthUrl = loo_Oauth.StartAuth()
if loo_Oauth.LastMethodSuccess <> 1 then
    Write-Debug "Could not start the local authorization listener."
    destroy loo_Oauth
    return
end if

li_Success = loo_Oauth.LaunchBrowser(ls_AuthUrl)
if li_Success <> 1 then
    loo_Oauth.Cancel()
    Write-Debug "Could not open the system browser."
    destroy loo_Oauth
    return
end if

li_Waited = 0
do while ((loo_Oauth.AuthFlowState < 3) AND (li_Waited < 180000))
    loo_Oauth.SleepMs(200)
    li_Waited = li_Waited + 200
loop
if loo_Oauth.AuthFlowState < 3 then
    loo_Oauth.Cancel()
    Write-Debug "Authorization timed out."
    destroy loo_Oauth
    return
end if

if loo_Oauth.AuthFlowState <> 3 then
    Write-Debug "Authorization did not complete. Flow state: " + string(loo_Oauth.AuthFlowState)
    destroy loo_Oauth
    return
end if

loo_Token = create oleobject
li_rc = loo_Token.ConnectToNewObject("Chilkat.JsonObject")

loo_Token.Strict = 1
li_Success = loo_Token.Load(loo_Oauth.AccessTokenResponse)
if li_Success <> 1 then
    Write-Debug "Could not parse the token response; existing cache unchanged."
    destroy loo_Oauth
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("access_token") <> 1) OR (loo_Token.StringOfEquals("access_token","",1) = 1) then
    Write-Debug "Expected a nonempty access_token in the JSON."
    destroy loo_Oauth
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("refresh_token") <> 1) OR (loo_Token.StringOfEquals("refresh_token","",1) = 1) then
    Write-Debug "Expected a nonempty refresh_token in the JSON."
    destroy loo_Oauth
    destroy loo_Token
    return
end if

if (loo_Token.JsonTypeOf("expires_in") <> 2) OR (loo_Token.IntOf("expires_in") <= 0) then
    Write-Debug "Expected a positive access-token lifetime."
    destroy loo_Oauth
    destroy loo_Token
    return
end if

loo_Obtained = create oleobject
li_rc = loo_Obtained.ConnectToNewObject("Chilkat.CkDateTime")

li_Success = loo_Obtained.SetFromCurrentSystemTime()
if li_Success <> 1 then
    Write-Debug "Could not record the token acquisition time."
    destroy loo_Oauth
    destroy loo_Token
    destroy loo_Obtained
    return
end if

loo_Token.UpdateString("obtained_at",loo_Obtained.GetAsTimestamp(0))
//  This file contains credentials. Choose an access-controlled directory for the signed-in account.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

li_Success = loo_Fac.DirEnsureExists("qa_data/tokens")
if li_Success <> 1 then
    Write-Debug "Could not create the private token-cache directory."
    destroy loo_Oauth
    destroy loo_Token
    destroy loo_Obtained
    destroy loo_Fac
    return
end if

li_Success = loo_Token.WriteFile("qa_data/tokens/outlookCalendar.json")
if li_Success <> 1 then
    Write-Debug "Could not save the token cache."
    destroy loo_Oauth
    destroy loo_Token
    destroy loo_Obtained
    destroy loo_Fac
    return
end if

//  Never print access_token, refresh_token, or the complete token response.
Write-Debug "Calendar authorization completed; token cache saved."


destroy loo_Oauth
destroy loo_Token
destroy loo_Obtained
destroy loo_Fac