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

Outlook Contacts: Authorize with OAuth2 and PKCE

See more Outlook Contact Examples

Authorize a public desktop application with the Microsoft identity platform v2.0 authorization-code flow and PKCE (S256). This works for supported personal Microsoft accounts and Microsoft 365 work/school accounts with a mailbox; it is not necessary to use an older OAuth endpoint for organizational accounts.

  1. Register an app in Microsoft Entra and choose the supported account types. Copy its Application (client) ID.
  2. Register http://localhost:3017/ under Mobile and desktop applications. Chilkat receives this local callback; no hosted web server is required.
  3. Request delegated Contacts.ReadWrite for this collection, or Contacts.Read for read-only access. Tenant consent policy may require administrator approval.
  4. Use common for an appropriately configured app supporting the intended accounts, consumers for personal accounts only, organizations for organizational accounts, or a tenant ID for one organization. Change both authority endpoints together.

A public desktop app cannot keep a client secret. The example enables PKCE, uses the system browser, and requests offline_access for a refresh token. It validates the token response before saving and does not print access tokens, refresh tokens, or the full response. Unrelated Files, profile, and user-write permissions are omitted.

The private cache contains obtained_at and expires_in for access-token lifetime handling. Refresh with the same client ID and authority; save a replacement refresh token when returned. Revocation or an interaction requirement means interactive sign-in is needed again. Use protected, atomic credential storage per account in production. The loopback browser flow is for interactive clients; web, mobile, and unattended applications need authentication and redirect handling appropriate to their environment.

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"
//  Contacts.ReadWrite covers creation and updates. Use Contacts.Read for read-only access.
loo_Oauth.Scope = "offline_access https://graph.microsoft.com/Contacts.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/outlookContacts.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 "Contacts authorization completed; token cache saved."


destroy loo_Oauth
destroy loo_Token
destroy loo_Obtained
destroy loo_Fac