Sample code for 30+ languages & platforms
SQL Server

Example: Http.SetAuthTokenSb method

Demonstrates the SetAuthTokenSb method.

Chilkat SQL Server Downloads

SQL Server
-- 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

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

    DECLARE @oauth2_access_token nvarchar(4000)
    SELECT @oauth2_access_token = 'ya39.Ci-XA_C5bGgRDC3UaD-h0_NeL-DVIQnI2gHtBBBHkZzrwlARkwX6R3O0PCDEzRlfaQ'

    DECLARE @sbAccessToken int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbAccessToken OUT

    EXEC sp_OAMethod @sbAccessToken, 'Append', @success OUT, @oauth2_access_token

    --  Causes the "Authorization: Bearer <access_token>" header to be added to all HTTP requests.
    EXEC sp_OAMethod @http, 'SetAuthTokenSb', @success OUT, @sbAccessToken

    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @http, 'QuickGetStr', @responseText OUT, 'https://chilkatsoft.com/helloWorld.txt'
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @sbAccessToken
        RETURN
      END

    --  See the HTTP request header that was sent.
    EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
    PRINT @sTmp0

    --  Output:
    --  
    --  GET /helloWorld.txt HTTP/1.1
    --  Host: chilkatsoft.com
    --  Accept: */*
    --  Accept-Encoding: gzip
    --  Authorization: Bearer ya39.Ci-XA_C5bGgRDC3UaD-h0_NeL-DVIQnI2gHtBBBHkZzrwlARkwX6R3O0PCDEzRlfaQ

    --  Note: Starting in v11.2.0, the information that should be kept secret in the Authorization header
    --  will be redacted.  Thus, in Chilkat v11.2.0 and later, the LastHeader property will show the following:

    --  Authorization: Bearer ****

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @sbAccessToken


END
GO