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

Amazon Cognito: Add, List, or Remove Group Memberships

See more Amazon Cognito Examples

Choose list, add, or remove for an existing user and existing group. List follows NextToken, including empty pages, and reports an explicit stop at the 1000-page demonstration bound. It lists this user's groups, not every group or member in the pool. Adding/removing membership requires cognito-idp:AdminAddUserToGroup or AdminRemoveUserFromGroup; listing requires AdminListGroupsForUser. Scope IAM access to the pool ARN.

Live change: membership can affect application authorization and Identity Pool role selection. Existing JWT claims do not change in place; newly issued tokens reflect current membership. Groups do not themselves grant arbitrary AWS permissions; Identity Pool role mappings and IAM policies determine AWS access.

Uses the Amazon Cognito User Pools regional JSON API with Chilkat.Rest and AuthAws (Signature Version 4, service cognito-idp). It does not use Identity Pools, the OAuth domain, or a Cognito bearer token. Set the region to match the pool; this example uses the standard AWS commercial endpoint. Other partitions have their own endpoint suffixes.

Supply a private qa_data/aws/cognito.json with string fields access_key, secret_key, and, for temporary IAM/STS credentials, session_token. Prefer short-lived role credentials and refresh them before expiry. The example reads this file explicitly; it does not automatically discover an AWS profile or role. The STS session token is sent in the documented X-Amz-Security-Token query parameter and included in the signature; keep request URLs and diagnostics private. Keep credentials outside public web content and source control. IAM administration can run in a controlled backoffice desktop application or a backend; never embed administrative AWS keys in publicly distributed software.

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

    --  Assumes Chilkat is unlocked. See Global Unlock Sample.
    SELECT @success = 0
    --  Private configuration, not a file in public web content or source control.
    --  Load current IAM credentials; these are NOT Cognito app-client credentials or JWTs.
    DECLARE @credentials int
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @credentials OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

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

        PRINT 'Could not load private AWS credentials.'
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'access_key'
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    IF (@iTmp0 <> 1) or (@sTmp0 = '')
      BEGIN

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

        PRINT 'Missing or invalid secret_key.'
        EXEC @hr = sp_OADestroy @credentials
        RETURN
      END
    --  Set this once to the region containing the user pool.
    DECLARE @region nvarchar(4000)
    SELECT @region = 'us-west-2'
    DECLARE @endpoint int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @endpoint OUT

    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, 'cognito-idp.'
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, @region
    EXEC sp_OAMethod @endpoint, 'Append', @success OUT, '.amazonaws.com'
    DECLARE @aws int
    EXEC @hr = sp_OACreate 'Chilkat.AuthAws', @aws OUT

    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'access_key'
    EXEC sp_OASetProperty @aws, 'AccessKey', @sTmp0
    EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'secret_key'
    EXEC sp_OASetProperty @aws, 'SecretKey', @sTmp0
    EXEC sp_OASetProperty @aws, 'Region', @region
    EXEC sp_OASetProperty @aws, 'ServiceName', 'cognito-idp'
    EXEC sp_OASetProperty @aws, 'SignatureVersion', 4
    DECLARE @rest int
    EXEC @hr = sp_OACreate 'Chilkat.Rest', @rest OUT

    EXEC sp_OASetProperty @rest, 'ConnectTimeoutMs', 20000
    EXEC sp_OASetProperty @rest, 'IdleTimeoutMs', 60000
    EXEC sp_OAMethod @rest, 'SetAuthAws', @success OUT, @aws
    IF @success <> 1
      BEGIN

        PRINT 'Could not configure AWS signing.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        RETURN
      END
    --  Temporary IAM/STS credentials require their session token on every signed request.
    --  AddQueryParam includes it in SigV4's canonical query. Never log the request URL.
    --  Leave session_token absent or empty only for credentials that do not have one.
    EXEC sp_OAMethod @credentials, 'HasMember', @iTmp0 OUT, 'session_token'
    IF @iTmp0 = 1
      BEGIN
        EXEC sp_OAMethod @credentials, 'JsonTypeOf', @iTmp0 OUT, 'session_token'
        IF @iTmp0 <> 1
          BEGIN

            PRINT 'session_token must be a string.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            RETURN
          END
        EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
        IF @sTmp0 <> ''
          BEGIN
            EXEC sp_OAMethod @credentials, 'StringOf', @sTmp0 OUT, 'session_token'
            EXEC sp_OAMethod @rest, 'AddQueryParam', @success OUT, 'X-Amz-Security-Token', @sTmp0
            IF @success <> 1
              BEGIN

                PRINT 'Could not set the session-token parameter.'
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                RETURN
              END
          END
      END
    --  Supply a Socket to explicitly enable TLS certificate verification.
    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 @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @rest, 'UseConnection', @success OUT, @connection, 1
    IF @success <> 1
      BEGIN

        PRINT 'Could not attach the TLS connection.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'Content-Type', 'application/x-amz-json-1.0'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set the content type.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        RETURN
      END

    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
    DECLARE @requestBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @requestBody OUT

    DECLARE @responseBody int
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @responseBody OUT

    DECLARE @userPoolId nvarchar(4000)
    SELECT @userPoolId = 'us-west-2_EXAMPLE'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'UserPoolId', @userPoolId
    --  Use the returned Cognito Username, not an assumed email alias.
    DECLARE @username nvarchar(4000)
    SELECT @username = 'example-user'
    EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'Username', @username
    DECLARE @action nvarchar(4000)
    SELECT @action = 'list'
    DECLARE @groupName nvarchar(4000)
    SELECT @groupName = 'ExampleGroup'
    IF (@action <> 'list') and (@action <> 'add') and (@action <> 'remove')
      BEGIN

        PRINT 'Choose list, add, or remove.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        RETURN
      END
    IF @action <> 'list'
      BEGIN
        EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'GroupName', @groupName
        DECLARE @operation nvarchar(4000)
        SELECT @operation = 'AWSCognitoIdentityProviderService.AdminAddUserToGroup'
        IF @action = 'remove'
          BEGIN
            SELECT @operation = 'AWSCognitoIdentityProviderService.AdminRemoveUserFromGroup'
          END
        EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', @operation
        IF @success <> 1
          BEGIN

            PRINT 'Could not select the membership operation.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            RETURN
          END
        --  EmitSb appends, so clear the buffer before each page/request.
        EXEC sp_OAMethod @requestBody, 'Clear', NULL
        EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
        IF @success <> 1
          BEGIN

            PRINT 'Could not encode the request.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            RETURN
          END
        --  Chilkat signs this POST, including the current headers and JSON body.
        EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
        IF @success <> 1
          BEGIN
            --  A failed exchange can leave the outcome of a write unknown. Do not blindly retry it.

            PRINT 'Request did not complete; inspect LastErrorText privately.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            RETURN
          END
        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            PRINT 'Cognito HTTP status: ' + @iTmp0
            --  AWS commonly reports throttling and expired credentials as HTTP 400, not 429/401.
            EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
            IF @success = 1
              BEGIN

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                PRINT 'AWS error type: ' + @sTmp0
                --  Inspect message and response headers privately; they can contain account/user data.
              END
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            RETURN
          END

        PRINT 'Membership change accepted.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        RETURN
      END
    EXEC sp_OAMethod @rest, 'AddHeader', @success OUT, 'X-Amz-Target', 'AWSCognitoIdentityProviderService.AdminListGroupsForUser'
    IF @success <> 1
      BEGIN

        PRINT 'Could not set the Cognito operation.'
        EXEC @hr = sp_OADestroy @credentials
        EXEC @hr = sp_OADestroy @endpoint
        EXEC @hr = sp_OADestroy @aws
        EXEC @hr = sp_OADestroy @rest
        EXEC @hr = sp_OADestroy @connection
        EXEC @hr = sp_OADestroy @body
        EXEC @hr = sp_OADestroy @result
        EXEC @hr = sp_OADestroy @requestBody
        EXEC @hr = sp_OADestroy @responseBody
        RETURN
      END
    EXEC sp_OAMethod @body, 'UpdateInt', @success OUT, 'Limit', 60
    --  Page size is not a total-result limit. Continue even after an empty page.
    DECLARE @nextToken nvarchar(4000)
    SELECT @nextToken = ''
    DECLARE @more int
    SELECT @more = 1
    DECLARE @page int
    SELECT @page = 0
    DECLARE @i int
    SELECT @i = 0
    DECLARE @count int
    SELECT @count = 0
    DECLARE @seenTokens int
    EXEC @hr = sp_OACreate 'Chilkat.Hashtable', @seenTokens OUT

    WHILE (@more = 1)
      BEGIN
        --  Demonstration bound: report partial results instead of silently truncating.
        IF @page = 1000
          BEGIN

            PRINT 'Stopped at 1000 pages; enumeration is incomplete. Resume with nextToken.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        --  EmitSb appends, so clear the buffer before each page/request.
        EXEC sp_OAMethod @requestBody, 'Clear', NULL
        EXEC sp_OAMethod @body, 'EmitSb', @success OUT, @requestBody
        IF @success <> 1
          BEGIN

            PRINT 'Could not encode the request.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        --  Chilkat signs this POST, including the current headers and JSON body.
        EXEC sp_OAMethod @rest, 'FullRequestSb', @success OUT, 'POST', '/', @requestBody, @responseBody
        IF @success <> 1
          BEGIN
            --  A failed exchange can leave the outcome of a write unknown. Do not blindly retry it.

            PRINT 'Request did not complete; inspect LastErrorText privately.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
        IF @iTmp0 <> 200
          BEGIN

            EXEC sp_OAGetProperty @rest, 'ResponseStatusCode', @iTmp0 OUT
            PRINT 'Cognito HTTP status: ' + @iTmp0
            --  AWS commonly reports throttling and expired credentials as HTTP 400, not 429/401.
            EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
            IF @success = 1
              BEGIN

                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, '__type'
                PRINT 'AWS error type: ' + @sTmp0
                --  Inspect message and response headers privately; they can contain account/user data.
              END
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @result, 'LoadSb', @success OUT, @responseBody
        IF @success <> 1
          BEGIN

            PRINT 'Expected a well-formed JSON response.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Groups'
        IF @iTmp0 <> 4
          BEGIN

            PRINT 'Expected Groups array.'
            EXEC @hr = sp_OADestroy @credentials
            EXEC @hr = sp_OADestroy @endpoint
            EXEC @hr = sp_OADestroy @aws
            EXEC @hr = sp_OADestroy @rest
            EXEC @hr = sp_OADestroy @connection
            EXEC @hr = sp_OADestroy @body
            EXEC @hr = sp_OADestroy @result
            EXEC @hr = sp_OADestroy @requestBody
            EXEC @hr = sp_OADestroy @responseBody
            EXEC @hr = sp_OADestroy @seenTokens
            RETURN
          END
        EXEC sp_OAMethod @result, 'SizeOfArray', @count OUT, 'Groups'
        SELECT @i = 0
        WHILE @i < @count
          BEGIN
            EXEC sp_OASetProperty @result, 'I', @i
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'Groups[i].GroupName'
            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Groups[i].GroupName'
            IF (@iTmp0 <> 1) or (@sTmp0 = '')
              BEGIN

                PRINT 'Missing or invalid Groups[i].GroupName.'
                EXEC @hr = sp_OADestroy @credentials
                EXEC @hr = sp_OADestroy @endpoint
                EXEC @hr = sp_OADestroy @aws
                EXEC @hr = sp_OADestroy @rest
                EXEC @hr = sp_OADestroy @connection
                EXEC @hr = sp_OADestroy @body
                EXEC @hr = sp_OADestroy @result
                EXEC @hr = sp_OADestroy @requestBody
                EXEC @hr = sp_OADestroy @responseBody
                EXEC @hr = sp_OADestroy @seenTokens
                RETURN
              END

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Groups[i].GroupName'
            PRINT 'Group: ' + @sTmp0

            EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'Groups[i].RoleArn'
            PRINT 'Role ARN: ' + @sTmp0
            SELECT @i = @i + 1
          END
        SELECT @page = @page + 1
        SELECT @more = 0
        EXEC sp_OAMethod @result, 'HasMember', @iTmp0 OUT, 'NextToken'
        IF @iTmp0 = 1
          BEGIN
            EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'NextToken'
            IF @iTmp0 <> 6
              BEGIN
                EXEC sp_OAMethod @result, 'JsonTypeOf', @iTmp0 OUT, 'NextToken'
                EXEC sp_OAMethod @result, 'StringOf', @sTmp0 OUT, 'NextToken'
                IF (@iTmp0 <> 1) or (@sTmp0 = '')
                  BEGIN

                    PRINT 'Missing or invalid NextToken.'
                    EXEC @hr = sp_OADestroy @credentials
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @aws
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @body
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @requestBody
                    EXEC @hr = sp_OADestroy @responseBody
                    EXEC @hr = sp_OADestroy @seenTokens
                    RETURN
                  END
                EXEC sp_OAMethod @result, 'StringOf', @nextToken OUT, 'NextToken'
                EXEC sp_OAMethod @seenTokens, 'Contains', @iTmp0 OUT, @nextToken
                IF @iTmp0 = 1
                  BEGIN

                    PRINT 'Repeated pagination token; enumeration is incomplete.'
                    EXEC @hr = sp_OADestroy @credentials
                    EXEC @hr = sp_OADestroy @endpoint
                    EXEC @hr = sp_OADestroy @aws
                    EXEC @hr = sp_OADestroy @rest
                    EXEC @hr = sp_OADestroy @connection
                    EXEC @hr = sp_OADestroy @body
                    EXEC @hr = sp_OADestroy @result
                    EXEC @hr = sp_OADestroy @requestBody
                    EXEC @hr = sp_OADestroy @responseBody
                    EXEC @hr = sp_OADestroy @seenTokens
                    RETURN
                  END
                EXEC sp_OAMethod @seenTokens, 'AddInt', @success OUT, @nextToken, 1
                EXEC sp_OAMethod @body, 'UpdateString', @success OUT, 'NextToken', @nextToken
                SELECT @more = 1
              END
          END
      END

    PRINT 'Enumeration complete.'

    EXEC @hr = sp_OADestroy @credentials
    EXEC @hr = sp_OADestroy @endpoint
    EXEC @hr = sp_OADestroy @aws
    EXEC @hr = sp_OADestroy @rest
    EXEC @hr = sp_OADestroy @connection
    EXEC @hr = sp_OADestroy @body
    EXEC @hr = sp_OADestroy @result
    EXEC @hr = sp_OADestroy @requestBody
    EXEC @hr = sp_OADestroy @responseBody
    EXEC @hr = sp_OADestroy @seenTokens


END
GO