Sample code for 30+ languages & platforms
SQL Server

Configure OAuth 1.0a Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.

Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.

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 @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @bTls int
    SELECT @bTls = 1
    DECLARE @bAutoReconnect int
    SELECT @bAutoReconnect = 1
    EXEC sp_OAMethod @rest, 'Connect', @success OUT, 'example.com', 443, @bTls, @bAutoReconnect
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END

    --  Configure OAuth 1.0a request signing.  Provide the consumer credentials and the access token
    --  credentials; Chilkat computes the OAuth signature for each request.
    DECLARE @oauth1 int
    EXEC @hr = sp_OACreate 'Chilkat.OAuth1', @oauth1 OUT

    --  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    --  interactive prompt, environment variable, or a secrets vault.
    EXEC sp_OASetProperty @oauth1, 'ConsumerKey', 'CONSUMER_KEY'
    EXEC sp_OASetProperty @oauth1, 'ConsumerSecret', 'CONSUMER_SECRET'
    EXEC sp_OASetProperty @oauth1, 'Token', 'ACCESS_TOKEN'
    EXEC sp_OASetProperty @oauth1, 'TokenSecret', 'ACCESS_TOKEN_SECRET'
    EXEC sp_OASetProperty @oauth1, 'SignatureMethod', 'HMAC-SHA1'

    --  The 2nd argument selects whether OAuth parameters are placed in the query string (1) or the
    --  Authorization header (0).
    DECLARE @bUseQueryParams int
    SELECT @bUseQueryParams = 0
    EXEC sp_OAMethod @rest, 'SetAuthOAuth1', @success OUT, @oauth1, @bUseQueryParams
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth1
        RETURN
      END

    DECLARE @responseText nvarchar(4000)
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'GET', '/api/resource'
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @rest, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @oauth1
        RETURN
      END

    PRINT @responseText

    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @oauth1


END
GO