SQL Server Requires Chilkat v11.5.0+
SQL Server
Box: Get a Service Account Token with Client Credentials
See more Box Examples
Obtain a Box service-account token using the client credentials grant. Configure and authorize a Server app first. Load credentials from protected application configuration; do not ship a client secret in a public client.
See the Box API guide for setup and the complete workflow.
Chilkat SQL Server Downloads
--
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 @curl int
EXEC @hr = sp_OACreate 'Chilkat.HttpCurl', @curl OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'client_id', 'BOX_CLIENT_ID'
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'client_secret', 'BOX_CLIENT_SECRET'
EXEC sp_OAMethod @curl, 'SetVar', NULL, 'enterprise_id', 'BOX_ENTERPRISE_ID'
-- enterprise identifies the app's service account. This is not an admin-user token.
EXEC sp_OAMethod @curl, 'ClearTargetOutput', NULL, '*'
EXEC sp_OAMethod @curl, 'ClearVar', NULL, 'access_token'
EXEC sp_OAMethod @curl, 'AddTargetOutput', NULL, 'access_token', 'access_token'
EXEC sp_OAMethod @curl, 'DoYourThing', @success OUT, 'curl -X POST "https://api.box.com/oauth2/token" --data-urlencode "grant_type=client_credentials" --data-urlencode "client_id={{client_id}}" --data-urlencode "client_secret={{client_secret}}" --data-urlencode "box_subject_type=enterprise" --data-urlencode "box_subject_id={{enterprise_id}}"'
EXEC sp_OAGetProperty @curl, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) or (@iTmp0 <> 200)
BEGIN
EXEC sp_OAGetProperty @curl, 'StatusCode', @iTmp0 OUT
PRINT 'Box request failed. HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @curl, 'FailReason', @iTmp0 OUT
PRINT 'Failure reason: ' + @iTmp0
EXEC @hr = sp_OADestroy @curl
RETURN
END
EXEC sp_OAMethod @curl, 'VarDefined', @iTmp0 OUT, '!'
IF @iTmp0 <> 1
BEGIN
PRINT 'Expected response fields are missing.'
EXEC @hr = sp_OADestroy @curl
RETURN
END
-- Keep this token in protected application memory; reacquire it when it expires.
-- Do not print tokens or the complete token response.
PRINT 'Service-account access token obtained.'
EXEC sp_OAMethod @curl, 'ClearTargetOutput', NULL, '*'
DECLARE @authorizedCommand1 int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @authorizedCommand1 OUT
EXEC sp_OAMethod @authorizedCommand1, 'SetString', @success OUT, 'curl -X GET "https://api.box.com/2.0/users/me?fields=id,name,login" -H "Authorization: Bearer {{access_token}}"'
DECLARE @headerReplacement1 int
EXEC sp_OAMethod @curl, 'GetVar', @sTmp0 OUT, 'access_token'
EXEC sp_OAMethod @authorizedCommand1, 'Replace', @headerReplacement1 OUT, '{{access_token}}', @sTmp0
EXEC sp_OAMethod @authorizedCommand1, 'GetAsString', @sTmp0 OUT
EXEC sp_OAMethod @curl, 'DoYourThing', @success OUT, @sTmp0
EXEC sp_OAGetProperty @curl, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) or (@iTmp0 <> 200)
BEGIN
EXEC sp_OAGetProperty @curl, 'StatusCode', @iTmp0 OUT
PRINT 'Box request failed. HTTP status: ' + @iTmp0
EXEC sp_OAGetProperty @curl, 'FailReason', @iTmp0 OUT
PRINT 'Failure reason: ' + @iTmp0
EXEC sp_OAGetProperty @curl, 'ResponseBodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @authorizedCommand1
RETURN
END
EXEC sp_OAGetProperty @curl, 'ResponseBodyStr', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @curl
EXEC @hr = sp_OADestroy @authorizedCommand1
END
GO