SQL Server Requires Chilkat v11.0.0+
SQL Server
Schwab: List authorized accounts and select one
See more Schwab Examples
Lists masked account numbers. Run once to see their endings, then enter the last four digits of the intended account. A unique match saves Schwab’s hashValue in a separate secret for the account-specific examples. If two accounts share an ending, use the full-account input from your account picker. A failed match leaves the previous selection in place.
Use the access token saved by desktop authorization. Use the same Chilkat Secrets store as the OAuth example.
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
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
SELECT @success = 0
DECLARE @vault int
EXEC @hr = sp_OACreate 'Chilkat.Secrets', @vault OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @vault, 'Location', 'local_manager'
EXEC sp_OASetProperty @vault, 'AutoSplit', 1
DECLARE @entry int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @entry OUT
EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'appName', 'SchwabTrader'
EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'service', 'OAuth2'
EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'username', 'personal-desktop'
DECLARE @tokens int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @tokens OUT
EXEC sp_OAMethod @vault, 'GetSecretJson', @success OUT, @entry, @tokens
IF @success <> 1
BEGIN
PRINT 'Could not read the token record. Check the store and complete desktop authorization first.'
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
RETURN
END
DECLARE @accessToken nvarchar(4000)
EXEC sp_OAMethod @tokens, 'StringOf', @accessToken OUT, 'access_token'
IF @accessToken = ''
BEGIN
PRINT 'Run the Schwab authorization or refresh example to save an access token.'
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
RETURN
END
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
EXEC sp_OASetProperty @http, 'AuthToken', @accessToken
-- Supply one of these from your account picker. Leave both empty to list only.
DECLARE @selectedEnding nvarchar(4000)
SELECT @selectedEnding = ''
DECLARE @selectedFullNumber nvarchar(4000)
SELECT @selectedFullNumber = ''
DECLARE @resp int
EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT
EXEC sp_OAMethod @http, 'HttpNoBody', @success OUT, 'GET', 'https://api.schwabapi.com/trader/v1/accounts/accountNumbers', @resp
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF (@success <> 1) or (@iTmp0 <> 200)
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
PRINT 'No HTTP response. Check connectivity.'
END
ELSE
BEGIN
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
PRINT 'Schwab returned HTTP ' + @iTmp0
EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
IF @iTmp0 = 401
BEGIN
PRINT 'Refresh the access token or repeat desktop authorization.'
END
END
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
RETURN
END
DECLARE @accounts int
EXEC @hr = sp_OACreate 'Chilkat.JsonArray', @accounts OUT
EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
EXEC sp_OAMethod @accounts, 'Load', @success OUT, @sTmp0
IF @success <> 1
BEGIN
PRINT 'Schwab returned unexpected JSON.'
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @accounts
RETURN
END
DECLARE @row int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @row OUT
DECLARE @number int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @number OUT
DECLARE @matches int
SELECT @matches = 0
DECLARE @i int
SELECT @i = 0
DECLARE @selectedHash nvarchar(4000)
SELECT @selectedHash = ''
EXEC sp_OAGetProperty @accounts, 'Size', @iTmp0 OUT
WHILE (@i < @iTmp0)
BEGIN
EXEC sp_OAMethod @accounts, 'ObjectAt2', @success OUT, @i, @row
DECLARE @accountNumber nvarchar(4000)
EXEC sp_OAMethod @row, 'StringOf', @accountNumber OUT, 'accountNumber'
EXEC sp_OAMethod @number, 'SetString', @success OUT, @accountNumber
EXEC sp_OAGetProperty @number, 'Length', @iTmp0 OUT
IF @iTmp0 >= 4
BEGIN
DECLARE @start int
EXEC sp_OAGetProperty @number, 'Length', @iTmp0 OUT
SELECT @start = @iTmp0 - 4
DECLARE @ending nvarchar(4000)
EXEC sp_OAMethod @number, 'GetRange', @ending OUT, @start, 4, 0
PRINT 'Account ending in ' + @ending
DECLARE @matched int
SELECT @matched = 0
IF @selectedFullNumber <> ''
BEGIN
IF @accountNumber = @selectedFullNumber
BEGIN
SELECT @matched = 1
END
END
ELSE
BEGIN
IF @ending = @selectedEnding
BEGIN
SELECT @matched = 1
END
END
IF @matched = 1
BEGIN
SELECT @matches = @matches + 1
EXEC sp_OAMethod @row, 'StringOf', @selectedHash OUT, 'hashValue'
END
END
SELECT @i = @i + 1
END
IF (@matches <> 1) or (@selectedHash = '')
BEGIN
PRINT 'Select exactly one account. For duplicate endings use the full-account input.'
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @accounts
EXEC @hr = sp_OADestroy @row
EXEC @hr = sp_OADestroy @number
RETURN
END
EXEC sp_OAMethod @entry, 'UpdateString', @success OUT, 'service', 'SelectedAccount'
DECLARE @selection int
EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @selection OUT
EXEC sp_OAMethod @selection, 'UpdateString', @success OUT, 'hashValue', @selectedHash
EXEC sp_OAMethod @vault, 'UpdateSecretJson', @success OUT, @entry, @selection
IF @success <> 1
BEGIN
PRINT 'Could not save the selected account.'
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @accounts
EXEC @hr = sp_OADestroy @row
EXEC @hr = sp_OADestroy @number
EXEC @hr = sp_OADestroy @selection
RETURN
END
PRINT 'Selected account saved.'
EXEC @hr = sp_OADestroy @vault
EXEC @hr = sp_OADestroy @entry
EXEC @hr = sp_OADestroy @tokens
EXEC @hr = sp_OADestroy @http
EXEC @hr = sp_OADestroy @resp
EXEC @hr = sp_OADestroy @accounts
EXEC @hr = sp_OADestroy @row
EXEC @hr = sp_OADestroy @number
EXEC @hr = sp_OADestroy @selection
END
GO