SQL Server Requires Chilkat v11.5.0+
SQL Server
Box: Authorize a User with OAuth 2.0
See more Box Examples
Interactive local-development example using a browser and a loopback callback. Register exactly http://localhost:3017/ in the Box User app. Run this in a trusted environment that can hold the client secret. A deployed web app must handle its registered HTTPS callback on the server.
See the Box API guide for setup and the complete workflow.
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 @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 @oauth2 int
EXEC @hr = sp_OACreate 'Chilkat.OAuth2', @oauth2 OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @oauth2, 'ListenPort', 3017
EXEC sp_OASetProperty @oauth2, 'AuthorizationEndpoint', 'https://account.box.com/api/oauth2/authorize'
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'
-- Use Box's documented confidential-client authorization-code flow.
EXEC sp_OASetProperty @oauth2, 'CodeChallenge', 0
DECLARE @url nvarchar(4000)
EXEC sp_OAMethod @oauth2, 'StartAuth', @url OUT
EXEC sp_OAGetProperty @oauth2, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 <> 1
BEGIN
EXEC sp_OAGetProperty @oauth2, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @oauth2
RETURN
END
EXEC sp_OAMethod @oauth2, 'LaunchBrowser', @success OUT, @url
IF @success <> 1
BEGIN
EXEC sp_OAGetProperty @oauth2, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @oauth2
RETURN
END
DECLARE @elapsedMs int
SELECT @elapsedMs = 0
EXEC sp_OAGetProperty @oauth2, 'AuthFlowState', @iTmp0 OUT
WHILE (@elapsedMs < 120000) and (@iTmp0 < 3)
BEGIN
EXEC sp_OAMethod @oauth2, 'SleepMs', NULL, 100
SELECT @elapsedMs = @elapsedMs + 100
END
EXEC sp_OAGetProperty @oauth2, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 < 3
BEGIN
EXEC sp_OAMethod @oauth2, 'Cancel', @success OUT
PRINT 'Authorization timed out.'
EXEC @hr = sp_OADestroy @oauth2
RETURN
END
EXEC sp_OAGetProperty @oauth2, 'AuthFlowState', @iTmp0 OUT
IF @iTmp0 <> 3
BEGIN
EXEC sp_OAGetProperty @oauth2, 'AuthFlowState', @iTmp0 OUT
PRINT 'Authorization was denied or failed. State: ' + @iTmp0
EXEC @hr = sp_OADestroy @oauth2
RETURN
END
-- Retain both tokens in your application's protected token store.
-- The variables below are deliberately not printed.
DECLARE @accessToken nvarchar(4000)
EXEC sp_OAGetProperty @oauth2, 'AccessToken', @accessToken OUT
DECLARE @refreshToken nvarchar(4000)
EXEC sp_OAGetProperty @oauth2, 'RefreshToken', @refreshToken OUT
-- For this local-development example only, save in a directory restricted to this user.
-- Create qa_data/tokens before running; never put this file under a web root.
DECLARE @tokens int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @tokens OUT
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 @oauth2
EXEC @hr = sp_OADestroy @tokens
RETURN
END
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 @oauth2
EXEC @hr = sp_OADestroy @tokens
RETURN
END
PRINT 'Authorization complete; token response saved in the protected local token file.'
EXEC @hr = sp_OADestroy @oauth2
EXEC @hr = sp_OADestroy @tokens
END
GO