Sample code for 30+ languages & platforms
SQL Server Requires Chilkat v11.6.0+

Azure Storage: Authenticate with Entra Client Credentials

See more Azure Cloud Storage Examples

Supply account, tenant_id (directory GUID), client_id and client_secret in private qa_data/azure/storage.json. The confidential application obtains an app-only token with the https://storage.azure.com/.default scope, then reads blob properties. Assign a Blob data role, such as Storage Blob Data Reader; a management-plane Contributor role alone does not grant Blob data access. Use this secret-based flow only where the application can keep its credentials private; certificates or managed identities are alternatives.

The token is saved to private qa_data/azure/storage-token.json with an expires_at timestamp for the related examples. Cache it until shortly before expiration, then repeat token acquisition. This flow does not use a refresh token. A 401 and a 403 need different investigation: token validity/audience versus data permissions or account policy. Allow time for RBAC changes to propagate.

Uses direct Chilkat.Rest requests and Azure public-cloud endpoints with verified HTTPS. REST requests select service version 2026-04-06. Create local input/output directories before running. Account-key examples read account and access_key string fields from private qa_data/azure/storage.json; do not embed secrets in distributed applications. Entra examples explain their own configuration. Keep credentials, bearer tokens, SAS URLs and diagnostic dumps private. Shared Key is supported only where allowed by account policy; Entra authentication is preferred where available.

Transport success and HTTP status are checked separately. Writes are not automatically replayed after an uncertain response. IDs, ETags, continuation markers and file sizes are strings; counters and explicitly bounded offsets use int. No webhook or web server is required.

Microsoft reference.

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

    --  StringBuilder compares text by value in every generated language.
    DECLARE @textCompare int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @textCompare OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    DECLARE @config int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @config OUT

    EXEC sp_OASetProperty @config, 'Strict', 1
    EXEC sp_OAMethod @config, 'LoadFile', @success OUT, 'qa_data/azure/storage.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load private Azure configuration.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText1 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText1 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'account'
    IF (@iTmp0 <> 1) or (@sameText1 = 1)
      BEGIN

        PRINT 'Supply the storage account name.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        RETURN
      END
    DECLARE @identity int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @identity OUT

    EXEC sp_OASetProperty @identity, 'ConnectTimeoutMs', 20000
    EXEC sp_OASetProperty @identity, 'IdleTimeoutMs', 60000
    DECLARE @expires int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @expires OUT

    EXEC sp_OAMethod @expires, 'SetFromCurrentSystemTime', @success OUT
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'tenant_id'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText2 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText2 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'tenant_id'
    IF (@iTmp0 <> 1) or (@sameText2 = 1)
      BEGIN

        PRINT 'Supply tenant_id in private storage.json.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_id'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText3 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText3 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
    IF (@iTmp0 <> 1) or (@sameText3 = 1)
      BEGIN

        PRINT 'Supply client_id in private storage.json.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_secret'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText4 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText4 OUT, '', 1
    EXEC sp_OAMethod @config, 'JsonTypeOf', @iTmp0 OUT, 'client_secret'
    IF (@iTmp0 <> 1) or (@sameText4 = 1)
      BEGIN

        PRINT 'Supply client_secret in private storage.json.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        RETURN
      END
    DECLARE @identityTls int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @identityTls OUT

    EXEC sp_OASetProperty @identityTls, 'RequireSslCertVerify', 1
    EXEC sp_OAMethod @identityTls, 'Connect', @success OUT, 'login.microsoftonline.com', 443, 1, 20000
    IF @success <> 1
      BEGIN

        PRINT 'Identity TLS connection failed.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        RETURN
      END
    EXEC sp_OAMethod @identity, 'UseConnection', @success OUT, @identityTls, 0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        RETURN
      END
    DECLARE @tokenPath int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @tokenPath OUT

    --  tenant_id is the application's directory GUID, not common or organizations.
    EXEC sp_OAMethod @tokenPath, 'Append', @success OUT, '/'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'tenant_id'
    EXEC sp_OAMethod @tokenPath, 'Append', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    EXEC sp_OAMethod @tokenPath, 'Append', @success OUT, '/oauth2/v2.0/token'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    EXEC sp_OAMethod @identity, 'AddQueryParam', @success OUT, 'grant_type', 'client_credentials'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_id'
    EXEC sp_OAMethod @identity, 'AddQueryParam', @success OUT, 'client_id', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'client_secret'
    EXEC sp_OAMethod @identity, 'AddQueryParam', @success OUT, 'client_secret', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    EXEC sp_OAMethod @identity, 'AddQueryParam', @success OUT, 'scope', 'https://storage.azure.com/.default'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    --  FullRequestFormUrlEncoded serializes these parameters in the POST body.
    DECLARE @tokenText nvarchar(4000)
    EXEC sp_OAMethod @tokenPath, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @identity, 'FullRequestFormUrlEncoded', @tokenText OUT, 'POST', @sTmp0
    EXEC sp_OAGetProperty @identity, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Token request did not complete; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    EXEC sp_OAGetProperty @identity, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        PRINT 'Token request failed; inspect the status privately, never print the response token.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        RETURN
      END
    DECLARE @token int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @token OUT

    EXEC sp_OASetProperty @token, 'Strict', 1
    EXEC sp_OAMethod @token, 'Load', @success OUT, @tokenText
    IF @success <> 1
      BEGIN

        PRINT 'Expected token JSON.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'access_token'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText5 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText5 OUT, '', 1
    EXEC sp_OAMethod @textCompare, 'Clear', NULL
    EXEC sp_OAMethod @token, 'StringOf', @sTmp0 OUT, 'token_type'
    EXEC sp_OAMethod @textCompare, 'SetString', @success OUT, @sTmp0
    DECLARE @sameText6 int
    EXEC sp_OAMethod @textCompare, 'ContentsEqual', @sameText6 OUT, 'Bearer', 1
    EXEC sp_OAMethod @token, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    IF (@iTmp0 <> 1) or (@sameText5 = 1) or (@sameText6 <> 1)
      BEGIN

        PRINT 'Missing bearer token.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    DECLARE @seconds int
    EXEC sp_OAMethod @token, 'IntOf', @seconds OUT, 'expires_in'
    IF (@seconds < 120) or (@seconds > 86400)
      BEGIN

        PRINT 'Unexpected token lifetime.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @expires, 'AddSeconds', @success OUT, @seconds
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @expires, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @token, 'UpdateString', @success OUT, 'expires_at', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    EXEC sp_OAMethod @token, 'WriteFile', @success OUT, 'qa_data/azure/storage-token.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not persist token privately.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        RETURN
      END
    DECLARE @accessToken nvarchar(4000)
    EXEC sp_OAMethod @token, 'StringOf', @accessToken OUT, 'access_token'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @config, 'StringOf', @sTmp0 OUT, 'account'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        RETURN
      END
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.blob.core.windows.net'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        RETURN
      END
    --  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
    --  Keep slash separators; names and paths are case-sensitive.
    DECLARE @resourcePath nvarchar(4000)
    SELECT @resourcePath = '/examplecontainer/example.bin'
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
    EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
    DECLARE @connection int
    EXEC @hr = sp_OACreate 'Chilkat.Socket', @connection OUT

    EXEC sp_OASetProperty @connection, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @connection, 'MaxReadIdleMs', 60000
    EXEC sp_OASetProperty @connection, 'MaxSendIdleMs', 60000
    EXEC sp_OAMethod @endpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @connection, 'Connect', @success OUT, @sTmp0, 443, 1, 20000
    IF @success <> 1
      BEGIN

        PRINT 'TLS connection failed; inspect diagnostics privately.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    --  Do not replay a write automatically after losing its response.
    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    DECLARE @responseText nvarchar(4000)

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

    EXEC sp_OASetProperty @result, 'Strict', 1
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'x-ms-version', '2026-04-06'
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    DECLARE @bearer int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @bearer OUT

    EXEC sp_OAMethod @bearer, 'Append', @success OUT, 'Bearer '
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        RETURN
      END
    EXEC sp_OAMethod @bearer, 'Append', @success OUT, @accessToken
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        RETURN
      END
    EXEC sp_OAMethod @bearer, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Authorization', @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Could not prepare the request.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        RETURN
      END
    EXEC sp_OAMethod @rest, 'FullRequestNoBody', @responseText OUT, 'HEAD', @resourcePath
    EXEC sp_OAGetProperty @rest, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 <> 1
      BEGIN

        PRINT 'Request did not complete; outcome may be uncertain.'
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        RETURN
      END
    EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        PRINT 'HTTP status: ' + @iTmp0

        EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-error-code'
        PRINT 'Azure error code: ' + @sTmp0

        EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'x-ms-request-id'
        PRINT 'Request ID: ' + @sTmp0
        --  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
        EXEC @hr = sp_OADestroy @textCompare
        EXEC @hr = sp_OADestroy @config
        EXEC @hr = sp_OADestroy @identity
        EXEC @hr = sp_OADestroy @expires
        EXEC @hr = sp_OADestroy @identityTls
        EXEC @hr = sp_OADestroy @tokenPath
        EXEC @hr = sp_OADestroy @token
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @bearer
        RETURN
      END

    PRINT 'Storage token obtained and blob properties read.'

    EXEC sp_OAMethod @rest, 'ResponseHdrByName', @sTmp0 OUT, 'ETag'
    PRINT 'ETag: ' + @sTmp0

    EXEC @hr = sp_OADestroy @textCompare
    EXEC @hr = sp_OADestroy @config
    EXEC @hr = sp_OADestroy @identity
    EXEC @hr = sp_OADestroy @expires
    EXEC @hr = sp_OADestroy @identityTls
    EXEC @hr = sp_OADestroy @tokenPath
    EXEC @hr = sp_OADestroy @token
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @bearer


END
GO