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.
- 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.
- 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. - Add the delegated Microsoft Graph
Calendars.ReadWritepermission for this example collection. A basic read-only application can instead useCalendars.ReadBasic; useCalendars.Readwhen fuller event information is required. Tenant consent policies can require an administrator even for delegated access. - Use
commononly with the appropriate app account-type configuration,consumersfor personal accounts only,organizationsfor 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.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
DECLARE @iTmp0 int
DECLARE @iTmp1 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
DECLARE @oauth int
EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- 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).
EXEC sp_OASetProperty @oauth, 'AuthorizationEndpoint', 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
EXEC sp_OASetProperty @oauth, 'TokenEndpoint', 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
EXEC sp_OASetProperty @oauth, 'ClientId', 'YOUR_APPLICATION_CLIENT_ID'
-- Public desktop applications cannot keep a client secret. Use PKCE instead.
EXEC sp_OASetProperty @oauth, 'LocalHost', 'localhost'
EXEC sp_OASetProperty @oauth, 'ListenPort', 3017
EXEC sp_OASetProperty @oauth, 'CodeChallenge', 1
EXEC sp_OASetProperty @oauth, 'CodeChallengeMethod', 'S256'
-- ReadWrite covers this collection's create/delete operations. Use ReadBasic for basic lists only.
EXEC sp_OASetProperty @oauth, 'Scope', 'offline_access https://graph.microsoft.com/Calendars.ReadWrite'
EXEC sp_OAMethod @oauth, 'AddAuthQueryParam', @success OUT, 'prompt', 'select_account'
IF @success <> 1
BEGIN
PRINT 'Could not configure the authorization request.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @authUrl nvarchar(4000)
EXEC sp_OAMethod @oauth, 'StartAuth', @authUrl OUT
EXEC sp_OAGetProperty @oauth, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
PRINT 'Could not start the local authorization listener.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAMethod @oauth, 'LaunchBrowser', @success OUT, @authUrl
IF @success <> 1
BEGIN
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Could not open the system browser.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @waited int
SELECT @waited = 0
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
WHILE ((@iTmp0 < 3) and (@waited < 180000))
BEGIN
EXEC sp_OAMethod @oauth, 'SleepMs', NULL, 200
SELECT @waited = @waited + 200
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 < 3
BEGIN
EXEC sp_OAMethod @oauth, 'Cancel', @success OUT
PRINT 'Authorization timed out.'
EXEC @hr = sp_OADestroy @oauth
RETURN
END
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 <> 3
BEGIN
EXEC sp_OAGetProperty @oauth, 'AuthFlowState', @iTmp0 OUT
PRINT 'Authorization did not complete. Flow state: ' + @iTmp0
EXEC @hr = sp_OADestroy @oauth
RETURN
END
DECLARE @token int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
EXEC sp_OASetProperty @token, 'Strict', 1
EXEC sp_OAGetProperty @oauth, 'AccessTokenResponse', @sTmp0 OUT
EXEC sp_OAMethod @token, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Could not parse the token response; existing cache unchanged.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty access_token in the JSON.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
EXEC sp_OAMethod @token, 'StringOfEquals', @iTmp1 OUT, 'refresh_token', '', 1
IF (@iTmp0 <> 1) or (@iTmp1 = 1)
BEGIN
PRINT 'Expected a nonempty refresh_token in the JSON.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'expires_in'
EXEC sp_OAMethod @token, 'IntOf', @iTmp1 OUT, 'expires_in'
IF (@iTmp0 <> 2) or (@iTmp1 <= 0)
BEGIN
PRINT 'Expected a positive access-token lifetime.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
RETURN
END
DECLARE @obtained int
EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @obtained OUT
EXEC sp_OAMethod @obtained, 'SetFromCurrentSystemTime', @success OUT
IF @success <> 1
BEGIN
PRINT 'Could not record the token acquisition time.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
RETURN
END
EXEC sp_OAMethod @obtained, 'GetAsTimestamp', @sTmp0 OUT, 0
EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'obtained_at', @sTmp0
-- This file contains credentials. Choose an access-controlled directory for the signed-in account.
DECLARE @fac int
EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT
EXEC sp_OAMethod @fac, 'DirEnsureExists', @success OUT, 'qa_data/tokens'
IF @success <> 1
BEGIN
PRINT 'Could not create the private token-cache directory.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
EXEC @hr = sp_OADestroy @fac
RETURN
END
EXEC sp_OAMethod @token, 'WriteFile', @success OUT, 'qa_data/tokens/outlookCalendar.json'
IF @success <> 1
BEGIN
PRINT 'Could not save the token cache.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
EXEC @hr = sp_OADestroy @fac
RETURN
END
-- Never print access_token, refresh_token, or the complete token response.
PRINT 'Calendar authorization completed; token cache saved.'
EXEC @hr = sp_OADestroy @oauth
EXEC @hr = sp_OADestroy @token
EXEC @hr = sp_OADestroy @obtained
EXEC @hr = sp_OADestroy @fac
END
GO