Sample code for 30+ languages & platforms
SQL Server

Example: Http.SetOAuthRsaKey method

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

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

    EXEC sp_OAMethod @pfx, 'LoadPfxFile', @success OUT, 'qa_data/pfx/MCD_Sandbox_chilkat_iccp_API_Keys/chilkat_iccp-sandbox.p12', 'keystorepassword'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        RETURN
      END

    DECLARE @privKey int
    EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT

    EXEC sp_OAMethod @pfx, 'PrivateKeyAt', @success OUT, 0, @privKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @pfx, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        EXEC @hr = sp_OADestroy @privKey
        RETURN
      END

    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    -- Use OAuth1.0a authentication.
    EXEC sp_OASetProperty @http, 'OAuth1', 1

    -- Use your own consumer key (this is not a valid consumer key)
    EXEC sp_OASetProperty @http, 'OAuthConsumerKey', '123abc'

    EXEC sp_OASetProperty @http, 'OAuthSigMethod', 'RSA-SHA256'

    EXEC sp_OAMethod @http, 'SetOAuthRsaKey', @success OUT, @privKey
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @http
        RETURN
      END

    -- Tell Chilkat to automatically calculate and add the oauth_body_hash field when sending the request.
    EXEC sp_OASetProperty @http, 'OAuthBodyHash', 1

    -- Send this request to an endpoint at chilkatsoft.com.  The purpose of this example is to show
    -- how the OAuth1.0a Authorization header is computed and sent by Chilkat.
    -- The chilkatsoft.com site itself doesn't do OAuth1.  It's just ignoring the Authorization header.
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

    EXEC sp_OAMethod @http, 'HttpStr', @success OUT, 'POST', 'https://chilkatsoft.com/echo_request_body.asp', '<notUsed>123</notUsed>', 'utf-8', 'application/xml', @resp
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @pfx
        EXEC @hr = sp_OADestroy @privKey
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        RETURN
      END

    -- Examine the request header we just sent..
    EXEC sp_OAGetProperty @http, 'LastHeader', @sTmp0 OUT
    PRINT @sTmp0

    -- Sample output:

    -- POST /echo_request_body.asp HTTP/1.1
    -- Host: chilkatsoft.com
    -- Accept: */*
    -- Accept-Encoding: gzip
    -- Content-Type: application/xml
    -- Content-Length: 22
    -- Authorization: OAuth oauth_consumer_key="123abc", oauth_nonce="A2E91C3B53E0BD7FBF71F441336679E358DDCEEE", oauth_body_hash="a5kPTsDwUwmBjC0voNlAAvM6YoaRS5X7sTO49jl3/h8=", oauth_timestamp="1756324932", oauth_signature_method="RSA-SHA256", oauth_version="1.0", oauth_signature="*****"

    EXEC @hr = sp_OADestroy @pfx
    EXEC @hr = sp_OADestroy @privKey
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp


END
GO