Box.com
Interact with the Box.com API using SQL Server
- Install the SQLHTTP database on your SQL Server
- Create the stored procedures documented below
- Execute the following SQL statement:
1 2 3 4 5 6 7 |
EXEC usp_Box_v2_Auth_Init @Profile = 'enter-profile-name-of-your-choosing', @ClientID = 'enter-your-client-id', @ClientSecret = 'enter-your-client-secret', @Email = 'enter-your-email-address', @RedirectURL = 'enter-you-redirecturl' --example: http://localhost:53200 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
CREATE PROCEDURE usp_Box_v2_Auth_Init( @Profile nvarchar(100), @ClientID varchar(50), @ClientSecret varchar(50), @Email varchar(50), @RedirectURL varchar(100)) AS SET NOCOUNT ON DECLARE @UserBrowseToURL varchar(MAX) DECLARE @QueryString nvarchar(MAX) EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'ClientID', @Value = @ClientID EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'ClientSecret', @Value = @ClientSecret EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'RedirectURL', @Value = @RedirectURL EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'Email', @Value = @Email SET @UserBrowseToURL = 'https://account.box.com/api/oauth2/authorize' EXEC SQLHTTP.net.QueryStringBuilder @QueryString OUTPUT, @Profile, 'response_type', 'code', 'client_id', @ClientID, 'box_login', '#Email' SET @QueryString = @QueryString + '&redirect_uri=' COLLATE SQL_Latin1_General_CP1_CI_AS + @RedirectURL SET @UserBrowseToURL = @UserBrowseToURL + @QueryString DECLARE @URL nvarchar(MAX) DECLARE @Body nvarchar(MAX) DECLARE @HTTPSessionID uniqueidentifier DECLARE @Response nvarchar(MAX) DECLARE @StatusCode int DECLARE @StatusDescription nvarchar(MAX) DECLARE @RedirectURLCode varchar(50) DECLARE @Timeout int DECLARE @TimeoutReached bit = 0 SET @Timeout = 180 --three minutes wait EXEC SQLHTTP.net.AuthListener @UserBrowseToURL = @UserBrowseToURL, @RedirectURL = @RedirectURL, @Timeout = @Timeout, @QueryString = @QueryString OUTPUT, @TimeoutReached = @TimeoutReached OUTPUT IF @TimeoutReached = 1 BEGIN RAISERROR('Timeout reached waiting for browser authentication', 16, 1) RETURN END SET @RedirectURLCode = SQLHTTP.net.Split(@QueryString, 'code=', 2) SET @URL = 'https://api.box.com/oauth2/token' EXEC SQLHTTP.net.FormDataBuilder @Body OUTPUT, @Profile, 'grant_type', 'authorization_code', 'code', @RedirectURLCode, 'client_id', @ClientID, 'client_secret', @ClientSecret SET @Body = @Body + '&redirect_uri=' COLLATE SQL_Latin1_General_CP1_CI_AS + @RedirectURL EXEC SQLHTTP.net.HTTPSession @HTTPSessionID OUTPUT EXEC SQLHTTP.net.HTTPRequest @HttpSessionID, @URL = @URL, @Method = 'POST', @Body = @Body, @StatusCode = @StatusCode OUTPUT, @StatusDescription = @StatusDescription OUTPUT, @Response = @Response OUTPUT IF @StatusCode >= 400 EXEC SQLHTTP.net.RaiseHttpError @StatusCode, @StatusDescription, @Response ELSE BEGIN DECLARE @BearerToken varchar(MAX) DECLARE @RefreshToken varchar(MAX) SELECT @BearerToken = [value] FROM SQLHTTP.net.Json_To_NodeTable(@Response) WHERE [Name] = 'access_token' SELECT @RefreshToken = [value] FROM SQLHTTP.net.Json_To_NodeTable(@Response) WHERE [Name] = 'refresh_token' EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'BearerToken', @value = @BearerToken EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'RefreshToken', @Value = @RefreshToken DECLARE @TokenCreatedDateTime nvarchar(100) SET @TokenCreatedDateTime = GetDate() EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'TokenCreatedDateTime', @Value = @TokenCreatedDateTime END GO |
1 2 3 4 5 6 7 8 9 10 |
CREATE PROCEDURE usp_Box_v2_Auth_Header( @Profile nvarchar(100), @HttpSessionID uniqueidentifier) AS DECLARE @AuthorizationHeaderValue varchar(MAX) SET @AuthorizationHeaderValue = 'Bearer ' + SQLHTTP.net.AuthParam(@Profile, 'BearerToken') EXEC SQLHTTP.net.RequestHeaderSet @HTTPSessionID, 'Authorization', @AuthorizationHeaderValue GO |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
CREATE PROCEDURE usp_Box_v2_Auth_Refresh( @Profile nvarchar(100)) AS DECLARE @URL nvarchar(MAX) DECLARE @HTTPSessionID uniqueidentifier DECLARE @Body nvarchar(MAX) DECLARE @Response nvarchar(MAX) DECLARE @StatusCode int DECLARE @StatusDescription nvarchar(MAX) SET @URL = 'https://api.box.com/oauth2/token' EXEC SQLHTTP.net.FormDataBuilder @Body OUTPUT, @Profile, 'grant_type', 'refresh_token', 'refresh_token', '#RefreshToken', 'client_id', '#ClientID', 'client_secret', '#ClientSecret' EXEC SQLHTTP.net.HTTPSession @HTTPSessionID OUTPUT EXEC SQLHTTP.net.HTTPRequest @HttpSessionID, @URL = @URL, @Method = 'POST', @Body = @Body, @StatusCode = @StatusCode OUTPUT, @StatusDescription = @StatusDescription OUTPUT, @Response = @Response OUTPUT IF @StatusCode >= 400 EXEC SQLHTTP.net.RaiseHttpError @StatusCode, @StatusDescription, @Response ELSE BEGIN DECLARE @BearerToken varchar(MAX) DECLARE @RefreshToken varchar(MAX) SELECT @BearerToken = [value] FROM SQLHTTP.net.Json_To_NodeTable(@Response) WHERE [Name] = 'access_token' SELECT @RefreshToken = [value] FROM SQLHTTP.net.Json_To_NodeTable(@Response) WHERE [Name] = 'refresh_token' EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'BearerToken', @value = @BearerToken EXEC SQLHTTP.net.AuthParamSet @Profile = @Profile, @Name = 'RefreshToken', @Value = @RefreshToken END GO |
- Box Content – Search
- Collaboration – Create
- Collaboration – Delete
- Collaboration – Fetch
- Collaboration – Update
- Collection Items – Add or Delete
- Collection Items – Fetch
- Collections – Fetch
- Comment – Create
- Comment – Delete
- Comment – Fetch
- Comment – Update
- Current User – Fetch
- Device Pin – Delete
- Device Pin – Fetch
- Enterprise Device Pins – Fetch
- Enterprise Events – Fetch
- Enterprise Templates – Fetch
- File – Copy
- File – Delete
- File – Download
- File – Lock or Unlock
- File – Upload
- File Collaborations – Fetch
- File Comments – Fetch
- File Embed Link – Fetch
- File Info – Fetch
- File Info – Update
- File Metadata Instance – Create
- File Metadata Instance – Fetch
- File Metadata Instances – Fetch
- File Tasks – Fetch
- File Thumbnail – Fetch
- File Watermark – Fetch
- File Watermark – Remove
- File Watermark – Update
- Folder – Copy
- Folder – Create
- Folder – Delete
- Folder – Fetch
- Folder – Update
- Folder Collaborations – Fetch
- Folder Info – Fetch
- Folder Metadata Instance – Create
- Folder Metadata Instance – Fetch
- Folder Metadata Instances – Fetch
- Folder Watermark – Fetch
- Folder Watermark – Remove
- Folder Watermark – Update
- Group – Create
- Group – Delete
- Group – Fetch
- Group – Update
- Group Membership – Create
- Group Membership – Delete
- Group Membership – Fetch
- Group Membership – Update
- Group Memberships – Fetch
- Groups – Fetch
- Metadata Template – Create
- Metadata Template – Fetch
- Metadata Template – Update
- Pending Collaborations – Fetch
- Recent Items – Fetch
- Task – Create
- Task – Fetch
- Task – Update
- Task Assignment – Create
- Task Assignment – Fetch
- Task Assignment – Update
- Task Assignments – Fetch
- Trashed Item – Delete
- Trashed Item – Fetch
- Trashed Item – Restore
- Trashed Items – Fetch
- User – Create
- User – Fetch
- User – Update
- User Events – Fetch
- User Memberships – Fetch
- Users – Fetch
- Web Link – Create
- Web Link – Delete
- Web Link – Fetch
- Web Link – Update
IMPORTANT DISCLAIMER
CODE/SQL ON THESE PAGES ARE PROVIDED AS-IS AND ARE AVAILABLE FOR ILLUSTRATIVE PURPOSES ONLY.
USERS ARE REQUIRED TO ABIDE BY THE TERMS AND CONDITIONS FOR USING REFERENCED THIRD PARTY WEBSITES AND/OR APIs FROM THEIR RESPECTIVE WEBSITES. WE DO NOT CONDONE ANY VIOLATION OF THIRD PARTY WEBSITES AND/OR APIs TERMS AND CONDITIONS USING OUR SOFTWARE.
USERS SHALL BE SOLELY RESPONSIBLE AND BE SOLELY LIABLE FOR VIOLATION OF ANY RULES SPECIFIED BY THIRD PARTIES FOR USING THEIR WEBSITES AND/OR APIs, OR INFRINGEMENT OF RIGHTS OF SUCH THIRD PARTIES.