Sample code for 30+ languages & platforms
SQL Server

Get Populi Access Key (Token)

See more Populi Examples

Demonstrates how to authenticate and obtain an access key using a username and password.

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
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

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

    -- Connect using TLS.
    -- A single REST object, once connected, can be used for many Populi REST API calls.
    -- The auto-reconnect indicates that if the already-established HTTPS connection is closed,
    -- then it will be automatically re-established as needed.
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'yourcollege.populi.co', 443, 1, @bAutoReconnect
    IF @success <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'username', 'POPULI_USERNAME'
    EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'password', 'POPULI_PASSWORD'

    DECLARE @responseBody nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestFormUrlEncoded', @responseBody OUT, 'POST', '/api/index.php'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    -- We should expect a 200 response if successful.
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        PRINT 'Request Header: '
        EXEC sp_OAGetProperty @rest, 'LastRequestHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT '----'

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'Response StatusCode = ' + @iTmp0

        EXEC sp_OAGetProperty @rest, 'ResponseStatusText', @sTmp0 OUT
        PRINT 'Response StatusLine: ' + @sTmp0

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @rest, 'ResponseHeader', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Response Body:'

        PRINT @responseBody
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    DECLARE @xml int
    EXEC @hr = sp_OACreate 'Chilkat.Xml', @xml OUT

    EXEC sp_OAMethod @xml, 'LoadXml', @success OUT, @responseBody
    EXEC sp_OAMethod @xml, 'GetXml', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample response:
    -- <?xml version="1.0" encoding="ISO-8859-1"?>
    -- <response>
    -- <access_key>63481e36023b5d ... 76d0dbc314</access_key>
    --     <accountid>19999999</accountid>
    --     <accounttype>PERSON</accounttype>
    -- </response>

    -- Save the XML to a file for subsequent requests..
    EXEC sp_OAMethod @xml, 'SaveXml', @success OUT, 'qa_data/tokens/populi_token.xml'

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @xml


END
GO