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

Outlook.com: 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/hotmail.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.

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/hotmail.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not read qa_data/tokens/hotmail.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/consumers/oauth2/v2.0/token'
    EXEC sp_OASetProperty @oauth, 'ClientId', 'CLIENT_ID'
    --  No client secret for the public desktop registration used by the PKCE example.
    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
        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 @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 @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/hotmail.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 @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 @fresh
    EXEC @hr = sp_OADestroy @obtained


END
GO