Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.0.0+

Microsoft 365: Refresh Mail OAuth2 Tokens

See more Office365 Examples

Exchange a saved delegated refresh token for a new access token without displaying a browser. Use the same app registration and authority as the original authorization. Save any rotated refresh token and the new lifetime information. On a rejected or malformed response, this example preserves the previous cache; an expired/revoked grant or interaction-required response needs interactive authorization again.

Read and update qa_data/tokens/office365.json in a private working directory. Serialize access to the cache; production storage should support atomic replacement and protect credentials. A successful refresh does not enable a disabled mail protocol. Client-credentials tokens have no refresh token; request a new application token instead.

Put {"client_id":"YOUR_APPLICATION_ID"} in private microsoft-oauth-client.json for a public desktop app. An existing confidential-client registration must also supply its client_secret from protected configuration; never distribute that secret in a public app. Replace common in the token endpoint with your tenant ID when matching a tenant-specific authorization.

Authorize first: desktop PKCE example. This is delegated background access after consent, not permanently unattended application authentication.

Microsoft reference documentation

Chilkat SQL Server Downloads

SQL Server
--
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.

    DECLARE @token int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OAMethod @token, 'LoadFile', @success OUT, 'qa_data/tokens/office365.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not read qa_data/tokens/office365.json from the private working directory.'
        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 'Authorize the account for offline access first.'
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    DECLARE @oauth int
    EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth OUT

    EXEC sp_OASetProperty @oauth, 'TokenEndpoint', 'https://login.microsoftonline.com/common/oauth2/v2.0/token'
    --  For public clients the private configuration contains only client_id.
    --  For an existing confidential client it also contains client_secret.
    DECLARE @client int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @client OUT

    EXEC sp_OAMethod @client, 'LoadFile', @success OUT, 'microsoft-oauth-client.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load the private OAuth client configuration.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @client
        RETURN
      END
    EXEC sp_OAMethod @client, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
    EXEC sp_OAMethod @client, 'StringOfEquals', @iTmp1 OUT, 'client_id', '', 1
    IF (@iTmp0 <> 1) or (@iTmp1 = 1)
      BEGIN

        PRINT 'A matching client_id is required.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @client
        RETURN
      END
    EXEC sp_OAMethod @client, 'StringOf', @sTmp0 OUT, 'client_id'
    EXEC sp_OASetProperty @oauth, 'ClientId', @sTmp0
    EXEC sp_OAMethod @client, 'JsonTypeOf', @iTmp0 OUT, 'client_secret'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @client, 'StringOf', @sTmp0 OUT, 'client_secret'
        EXEC sp_OASetProperty @oauth, 'ClientSecret', @sTmp0
      END
    --  Use the same registration and authority as the original authorization.
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'refresh_token'
    EXEC sp_OASetProperty @oauth, 'RefreshToken', @sTmp0
    EXEC sp_OAMethod @oauth, 'RefreshAccessToken', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Refresh failed. Check the OAuth client and grant. A revoked or expired grant requires fresh consent.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @client
        RETURN
      END
    DECLARE @fresh int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @fresh OUT

    EXEC sp_OAGetProperty @oauth, 'AccessTokenResponse', @sTmp0 OUT
    EXEC sp_OAMethod @fresh, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not parse the refresh response.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @client
        EXEC @hr = sp_OADestroy @fresh
        RETURN
      END
    EXEC sp_OAMethod @fresh, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    EXEC sp_OAMethod @fresh, 'StringOfEquals', @iTmp1 OUT, 'access_token', '', 1
    IF (@iTmp0 <> 1) or (@iTmp1 = 1)
      BEGIN

        PRINT 'The refresh response did not contain an access token.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @client
        EXEC @hr = sp_OADestroy @fresh
        RETURN
      END
    EXEC sp_OAMethod @fresh, 'JsonTypeOf', @iTmp0 OUT, 'refresh_token'
    EXEC sp_OAMethod @fresh, 'StringOfEquals', @iTmp1 OUT, 'refresh_token', '', 1
    IF (@iTmp0 <> 1) or (@iTmp1 = 1)
      BEGIN
        EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'refresh_token'
        EXEC sp_OAMethod @fresh, 'UpdateString', @success OUT, 'refresh_token', @sTmp0
      END
    EXEC sp_OAMethod @fresh, 'HasMember', @iTmp0 OUT, 'scope'
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp1 OUT, 'scope'
    IF (@iTmp0 <> 1) and (@iTmp1 = 1)
      BEGIN
        EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'scope'
        EXEC sp_OAMethod @fresh, 'UpdateString', @success OUT, 'scope', @sTmp0
      END
    --  Persist the rotated refresh token. Retain the previous one only if the response omits it.
    --  Use the new expires_in, not the old response's expiration information.
    DECLARE @obtained int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @obtained OUT

    EXEC sp_OAMethod @obtained, 'SetFromCurrentSystemTime', @success OUT
    EXEC sp_OAMethod @obtained, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @fresh, 'UpdateString', @success OUT, 'obtained_at', @sTmp0
    EXEC sp_OAMethod @fresh, 'WriteFile', @success OUT, 'qa_data/tokens/office365.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not save the refreshed credentials.'
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @oauth
        EXEC @hr = sp_OADestroy @client
        EXEC @hr = sp_OADestroy @fresh
        EXEC @hr = sp_OADestroy @obtained
        RETURN
      END

    PRINT 'Refreshed the access token.'

    EXEC @hr = sp_OADestroy @token
    EXEC @hr = sp_OADestroy @oauth
    EXEC @hr = sp_OADestroy @client
    EXEC @hr = sp_OADestroy @fresh
    EXEC @hr = sp_OADestroy @obtained


END
GO