Sample code for 30+ languages & platforms
SQL Server

curl Update Local Manager Secrets

See more CURL Examples

Demonstrates how to initially write secrets to the local manager that will later be used in curl commands. On Windows, this refers to the Windows Credential Manager, and on macOS, it refers to the Apple Keychain.

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
    -- 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 will store 3 secrets in the local manager.

    -- On Windows, this refers to the Windows Credential Manager, and on macOS, it refers to the Apple Keychain.
    -- These secrets will be used at a later point in time in curl commands,
    -- as shown in this example:  Using Local Manager Secrets with curl

    -- Also see: Chilkat v11.5.0 — Secrets Integration
    -- and also: Chilkat Secrets API

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

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'appName', 'sharepoint'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'service', 'oauth2'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'username', 'client_id'

    -- These are not actual/real values..
    EXEC sp_OAMethod @secrets, 'UpdateSecretStr', @success OUT, @json, '4afc67c5-6d3f-4ed0-91c7-5d239c78bff7'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @secrets, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @secrets
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'username', 'client_secret'
    EXEC sp_OAMethod @secrets, 'UpdateSecretStr', @success OUT, @json, 'Rlh8Q~xaP10Dw-goWQDLXrRJfYAFVW1hHauvfhO6'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @secrets, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @secrets
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'username', 'token_endpoint'
    EXEC sp_OAMethod @secrets, 'UpdateSecretStr', @success OUT, @json, 'https://login.microsoftonline.com/5f410b89-177f-40b3-9d66-ac519c728025/oauth2/v2.0/token'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @secrets, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @secrets
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    -- -----------------------------------------------------------------------------------------------------------
    -- The above secrets would be accessed like this:
    DECLARE @json2 int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json2 OUT

    EXEC sp_OASetProperty @json2, 'EnableSecrets', 1

    -- Chilkat sees the secret specification string (beginning with "!!") and resolves from the local manager.
    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'x', '!!sharepoint|oauth2|client_id'

    EXEC sp_OAMethod @json2, 'StringOf', @sTmp0 OUT, 'x'
    PRINT 'x = ' + @sTmp0

    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'y', '!!sharepoint|oauth2|client_secret'

    EXEC sp_OAMethod @json2, 'StringOf', @sTmp0 OUT, 'y'
    PRINT 'y = ' + @sTmp0

    EXEC sp_OAMethod @json2, 'UpdateString', @success OUT, 'z', '!!sharepoint|oauth2|token_endpoint'

    EXEC sp_OAMethod @json2, 'StringOf', @sTmp0 OUT, 'z'
    PRINT 'z = ' + @sTmp0

    -- You can see the values retrieved from the local manager:

    -- x = 4afc67c5-6d3f-4ed0-91c7-5d239c78bff7
    -- y = Rlh8Q~xaP10Dw-goWQDLXrRJfYAFVW1hHauvfhO6
    -- z = https://login.microsoftonline.com/5f410b89-177f-40b3-9d66-ac519c728025/oauth2/v2.0/token

    EXEC @hr = sp_OADestroy @secrets
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @json2


END
GO