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

Box: Refresh and Save Rotated OAuth Tokens

See more Box Examples

Refresh the user token saved by the authorization example. Box refresh tokens are single-use and expire after 60 days. Serialize refreshes for a token pair and save the replacement immediately; do not run concurrent refresh jobs.

See the Box API guide for setup and the complete workflow.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
    --  See Global Unlock Sample.
    --  Guide: Box API Guide.

    SELECT @success = 0

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

    EXEC sp_OAMethod @tokens, 'LoadFile', @success OUT, 'qa_data/tokens/box_oauth.json'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @tokens, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @tokens
        RETURN
      END
    DECLARE @oauth2 int
    EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth2 OUT

    EXEC sp_OASetProperty @oauth2, 'TokenEndpoint', 'https://api.box.com/oauth2/token'
    EXEC sp_OASetProperty @oauth2, 'ClientId', 'BOX_CLIENT_ID'
    EXEC sp_OASetProperty @oauth2, 'ClientSecret', 'BOX_CLIENT_SECRET'
    EXEC sp_OAMethod @tokens, 'StringOf', @sTmp0 OUT, 'refresh_token'
    EXEC sp_OASetProperty @oauth2, 'RefreshToken', @sTmp0
    EXEC sp_OAMethod @oauth2, 'RefreshAccessToken', @success OUT
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @oauth2, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END
    EXEC sp_OAGetProperty @oauth2, 'AccessTokenResponse', @sTmp0 OUT
    EXEC sp_OAMethod @tokens, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @tokens, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END
    --  The previous refresh token is now invalid. Preserve this response even if
    --  your normal token-store write fails. Do not retry with the old refresh token.
    EXEC sp_OAMethod @tokens, 'WriteFile', @success OUT, 'qa_data/tokens/box_oauth.json'
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @tokens, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @tokens
        EXEC @hr = sp_OADestroy @oauth2
        RETURN
      END

    PRINT 'New access and refresh tokens saved. Do not log their values.'

    EXEC @hr = sp_OADestroy @tokens
    EXEC @hr = sp_OADestroy @oauth2


END
GO