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

Amazon Cognito: Obtain a Machine-to-Machine Access Token

See more Amazon Cognito Examples

Create a dedicated confidential app client with a secret, client_credentials as its only OAuth flow, and allowed custom scopes from a configured resource server. Configure its client_id/domain in a separate app.json profile and put client_secret plus a space-separated custom scope string (for example api.example.com/read) in private m2m-client.json. Sends client_secret_basic authentication and a form-encoded token request. Keep this secret in a controlled backend or backoffice environment, not a public app.

This obtains authorization to your resource-server API, not IAM access to Cognito administration. It does not sign in a person, and Cognito returns no ID token or refresh token. Request another access token when needed, reuse it until near expiry, and check the granted scopes before calling your API. M2M authorization has its own Cognito pricing; do not request a fresh token for every API call.

Create qa_data/cognito/app.json with string fields region (for example us-west-2), user_pool_id, client_id, and account_key (your local account identifier). OAuth examples also require domain, the trusted HTTPS base URL of your Cognito domain, without a trailing slash. The region and user pool must match the app client. These examples use commercial AWS endpoints.

Sample cache/input files contain secrets. Use an access-controlled directory outside web content and source control, separate each local user/account, and serialize operations that modify the same cache. In a real application obtain passwords and codes through private UI instead of leaving them in files. Cache writes retain a .bak copy; protect and remove obsolete backups and challenge files after the flow. Production storage should provide atomic replacement and OS-backed secret protection. Never restore an old refresh token automatically after rotation. These examples do not require a hosted web server.

Checks transport completion, HTTP status, and required response fields before using results. Cognito often reports throttling, invalid codes, and authentication failures as HTTP 400. Present generic sign-in/recovery errors to users; keep detailed diagnostics private. Retry throttled reads with bounded backoff, but reconcile uncertain writes and refresh rotations before repeating them.

AWS documentation 1 | AWS documentation 2

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 @iTmp1 int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    --  App settings are trusted configuration. Keep a separate account_key/cache per local user.
    DECLARE @app int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @app OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @app, 'Strict', 1
    EXEC sp_OAMethod @app, 'LoadFile', @success OUT, 'qa_data/cognito/app.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load Cognito app settings.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'region'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid region.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'user_pool_id'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid user_pool_id.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'client_id'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid client_id.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'account_key'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid account_key.'
        EXEC @hr = sp_OADestroy @app
        RETURN
      END
    DECLARE @clientId nvarchar(4000)
    EXEC sp_OAMethod @app, 'StringOf', @clientId OUT, 'client_id'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'https://cognito-idp.'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'region'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com/'
    DECLARE @http int
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT

    EXEC sp_OASetProperty @http, 'RequireSslCertVerify', 1
    EXEC sp_OASetProperty @http, 'FollowRedirects', 0
    EXEC sp_OASetProperty @http, 'ConnectTimeout', 20
    EXEC sp_OASetProperty @http, 'ReadTimeout', 60
    DECLARE @resp int
    EXEC @hr = sp_OACreate 'Chilkat.HttpResponse', @resp OUT

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

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

    EXEC sp_OASetProperty @result, 'Strict', 1
    EXEC sp_OAMethod @app, 'JsonTypeOf', @iTmp0 OUT, 'domain'
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid domain.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        RETURN
      END
    --  This URL comes from trusted configuration, never from a token or user-supplied URL.
    DECLARE @tokenEndpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @tokenEndpoint OUT

    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'domain'
    EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, @sTmp0
    EXEC sp_OAMethod @tokenEndpoint, 'Append', @success OUT, '/oauth2/token'
    DECLARE @secret int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @secret OUT

    EXEC sp_OASetProperty @secret, 'Strict', 1
    EXEC sp_OAMethod @secret, 'LoadFile', @success OUT, 'qa_data/cognito/m2m-client.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not load qa_data/cognito/m2m-client.json.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        RETURN
      END
    EXEC sp_OAMethod @secret, 'JsonTypeOf', @iTmp0 OUT, 'client_secret'
    EXEC sp_OAMethod @secret, 'StringOf', @sTmp0 OUT, 'client_secret'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid client_secret.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        RETURN
      END
    EXEC sp_OAMethod @secret, 'JsonTypeOf', @iTmp0 OUT, 'scope'
    EXEC sp_OAMethod @secret, 'StringOf', @sTmp0 OUT, 'scope'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid scope.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        RETURN
      END
    --  Use a dedicated confidential M2M app client, not the public client from the user examples.
    EXEC sp_OASetProperty @http, 'Login', @clientId
    EXEC sp_OAMethod @secret, 'StringOf', @sTmp0 OUT, 'client_secret'
    EXEC sp_OASetProperty @http, 'Password', @sTmp0
    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    DECLARE @req int
    EXEC @hr = sp_OACreate 'Chilkat.HttpRequest', @req OUT

    EXEC sp_OASetProperty @req, 'HttpVerb', 'POST'
    EXEC sp_OASetProperty @req, 'ContentType', 'application/x-www-form-urlencoded'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'grant_type', 'client_credentials'
    EXEC sp_OAMethod @secret, 'StringOf', @sTmp0 OUT, 'scope'
    EXEC sp_OAMethod @req, 'AddParam', NULL, 'scope', @sTmp0
    EXEC sp_OAMethod @tokenEndpoint, 'GetAsString', @sTmp0 OUT
    EXEC sp_OAMethod @http, 'HttpReq', @success OUT, @sTmp0, @req, @resp
    EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
    IF @iTmp0 <> 200
      BEGIN

        EXEC sp_OAGetProperty @resp, 'StatusCode', @iTmp0 OUT
        PRINT 'HTTP status: ' + @iTmp0
        EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
        EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
        IF @success = 1
          BEGIN

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
            PRINT 'Error type: ' + @sTmp0

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'error'
            PRINT 'OAuth error: ' + @sTmp0
          END
        --  Inspect detailed diagnostics privately. Do not log passwords, sessions, or tokens.
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        RETURN
      END
    IF @success <> 1
      BEGIN

        PRINT 'The HTTP exchange did not complete.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        RETURN
      END
    EXEC sp_OAGetProperty @resp, 'BodyStr', @sTmp0 OUT
    EXEC sp_OAMethod @result, 'Load', @success OUT, @sTmp0
    IF @success <> 1
      BEGIN

        PRINT 'Expected a JSON response.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        RETURN
      END
    EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'access_token'
    EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'access_token'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

        PRINT 'Missing or invalid access_token.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        RETURN
      END
    EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'expires_in'
    EXEC sp_OAMethod @result, 'IntOf', @iTmp1 OUT, 'expires_in'
    IF (@iTmp0 <> 2) or (@iTmp1 <= 0)
      BEGIN

        PRINT 'Expected a positive token lifetime.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        RETURN
      END
    EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'token_type'
    IF @sTmp0 <> 'Bearer'
      BEGIN

        PRINT 'Expected a Bearer token.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        RETURN
      END
    --  Store separately from user tokens: there is no user ID token or refresh token.
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'client_id'
    EXEC sp_OAMethod @result, 'UpdateString', @success OUT, 'client_id', @sTmp0
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'user_pool_id'
    EXEC sp_OAMethod @result, 'UpdateString', @success OUT, 'user_pool_id', @sTmp0
    EXEC sp_OAMethod @app, 'StringOf', @sTmp0 OUT, 'account_key'
    EXEC sp_OAMethod @result, 'UpdateString', @success OUT, 'account_key', @sTmp0
    DECLARE @obtained int
    EXEC @hr = sp_OACreate 'Chilkat.CkDateTime', @obtained OUT

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

        PRINT 'Could not get the acquisition time.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @obtained
        RETURN
      END
    EXEC sp_OAMethod @obtained, 'GetAsTimestamp', @sTmp0 OUT, 0
    EXEC sp_OAMethod @result, 'UpdateString', @success OUT, 'obtained_at', @sTmp0
    DECLARE @resultM2Mtoken int
    EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @resultM2Mtoken OUT

    EXEC sp_OAMethod @resultM2Mtoken, 'DirEnsureExists', @success OUT, 'qa_data/tokens'
    IF @success <> 1
      BEGIN

        PRINT 'Could not create the private cache directory.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @obtained
        EXEC @hr = sp_OADestroy @resultM2Mtoken
        RETURN
      END
    EXEC sp_OAMethod @resultM2Mtoken, 'FileExists', @iTmp0 OUT, 'qa_data/tokens/cognito-m2m.json'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @resultM2Mtoken, 'FileCopy', @success OUT, 'qa_data/tokens/cognito-m2m.json', 'qa_data/tokens/cognito-m2m.json.bak', 0
        IF @success <> 1
          BEGIN

            PRINT 'Could not back up the existing cache.'
            EXEC @hr = sp_OADestroy @app
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @http
            EXEC @hr = sp_OADestroy @resp
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @tokenEndpoint
            EXEC @hr = sp_OADestroy @secret
            EXEC @hr = sp_OADestroy @req
            EXEC @hr = sp_OADestroy @obtained
            EXEC @hr = sp_OADestroy @resultM2Mtoken
            RETURN
          END
      END
    EXEC sp_OAMethod @result, 'WriteFile', @success OUT, 'qa_data/tokens/cognito-m2m.json'
    IF @success <> 1
      BEGIN

        PRINT 'Could not save M2M token; preserve any previous backup.'
        EXEC @hr = sp_OADestroy @app
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @resp
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @tokenEndpoint
        EXEC @hr = sp_OADestroy @secret
        EXEC @hr = sp_OADestroy @req
        EXEC @hr = sp_OADestroy @obtained
        EXEC @hr = sp_OADestroy @resultM2Mtoken
        RETURN
      END

    PRINT 'Machine access token saved privately.'

    EXEC @hr = sp_OADestroy @app
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @resp
    EXEC @hr = sp_OADestroy @body
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @tokenEndpoint
    EXEC @hr = sp_OADestroy @secret
    EXEC @hr = sp_OADestroy @req
    EXEC @hr = sp_OADestroy @obtained
    EXEC @hr = sp_OADestroy @resultM2Mtoken


END
GO