Skip to content

/AWS1/IF_CGP=>GETCLIENTTOKEN()

About GetClientToken

Issues an access token for machine-to-machine (M2M) authorization. Your app client provides its client ID and secret, and receives an access token that authorizes requests to your resource servers. GetClientToken provides the same functionality as the OAuth2 client-credentials grant; both authorize an application rather than a user.

To use this operation, you must configure the app client with a client secret and enable the ALLOW_CLIENT_TOKEN_AUTH authentication flow. The ALLOW_CLIENT_TOKEN_AUTH flow is mutually exclusive with user authentication flows. It must be the only authentication flow that you configure for the app client. For more information, see Scopes, M2M, and resource servers.

Amazon Cognito doesn't evaluate Identity and Access Management (IAM) policies in requests for this API operation. For this operation, you can't use IAM credentials to authorize requests, and you can't grant IAM permissions in policies. For more information about authorization models in Amazon Cognito, see Using the Amazon Cognito user pools API and user pool endpoints.

Method Signature

METHODS /AWS1/IF_CGP~GETCLIENTTOKEN
  IMPORTING
    !IV_CLIENTID TYPE /AWS1/CGPCLIENTIDTYPE OPTIONAL
    !IV_SECRET TYPE /AWS1/CGPCLIENTSECRETTYPE OPTIONAL
    !IT_SCOPES TYPE /AWS1/CL_CGPSCOPELISTTYPE_W=>TT_SCOPELISTTYPE OPTIONAL
    !IT_CLIENTMETADATA TYPE /AWS1/CL_CGPCLIENTMETTYPE_W=>TT_CLIENTMETADATATYPE OPTIONAL
  RETURNING
    VALUE(OO_OUTPUT) TYPE REF TO /aws1/cl_cgpgetclienttokenrsp
  RAISING
    /AWS1/CX_CGPFORBIDDENEXCEPTION
    /AWS1/CX_CGPINTERNALERROREX
    /AWS1/CX_CGPINVALIDPARAMETEREX
    /AWS1/CX_CGPNOTAUTHORIZEDEX
    /AWS1/CX_CGPOPNOTENABLEDEX
    /AWS1/CX_CGPRESOURCENOTFOUNDEX
    /AWS1/CX_CGPTOOMANYREQUESTSEX
    /AWS1/CX_CGPCLIENTEXC
    /AWS1/CX_CGPSERVEREXC
    /AWS1/CX_RT_TECHNICAL_GENERIC
    /AWS1/CX_RT_SERVICE_GENERIC.

IMPORTING

Required arguments:

iv_clientid TYPE /AWS1/CGPCLIENTIDTYPE /AWS1/CGPCLIENTIDTYPE

The ID of the app client that requests the access token. The app client must have a client secret and the ALLOW_CLIENT_TOKEN_AUTH authentication flow.

iv_secret TYPE /AWS1/CGPCLIENTSECRETTYPE /AWS1/CGPCLIENTSECRETTYPE

An active secret for the app client.

Optional arguments:

it_scopes TYPE /AWS1/CL_CGPSCOPELISTTYPE_W=>TT_SCOPELISTTYPE TT_SCOPELISTTYPE

The custom scopes to authorize in the access token, in the format resource-server-identifier/scope-name. Each scope must belong to a resource server in your user pool. If you don't specify any scopes, Amazon Cognito authorizes the scopes that are configured for the app client.

it_clientmetadata TYPE /AWS1/CL_CGPCLIENTMETTYPE_W=>TT_CLIENTMETADATATYPE TT_CLIENTMETADATATYPE

A map of custom key-value pairs that you can provide as input for any custom workflows that this action triggers. You create custom workflows by assigning Lambda functions to user pool triggers.

When Amazon Cognito invokes any of these functions, it passes a JSON payload, which the function receives as input. This payload contains a clientMetadata attribute that provides the data that you assigned to the ClientMetadata parameter in your request. In your function code, you can process the clientMetadata value to enhance your workflow for your specific needs.

To review the Lambda trigger types that Amazon Cognito invokes at runtime with API requests, see Connecting API actions to Lambda triggers in the Amazon Cognito Developer Guide.

When you use the ClientMetadata parameter, note that Amazon Cognito won't do the following:

  • Store the ClientMetadata value. This data is available only to Lambda triggers that are assigned to a user pool to support custom workflows. If your user pool configuration doesn't include triggers, the ClientMetadata parameter serves no purpose.

  • Validate the ClientMetadata value.

  • Encrypt the ClientMetadata value. Don't send sensitive information in this parameter.

RETURNING

oo_output TYPE REF TO /aws1/cl_cgpgetclienttokenrsp /AWS1/CL_CGPGETCLIENTTOKENRSP

Examples

Syntax Example

This is an example of the syntax for calling the method. It includes every possible argument and initializes every possible value. The data provided is not necessarily semantically accurate (for example the value "string" may be provided for something that is intended to be an instance ID, or in some cases two arguments may be mutually exclusive). The syntax shows the ABAP syntax for creating the various data structures.

DATA(lo_result) = lo_client->getclienttoken(
  it_clientmetadata = VALUE /aws1/cl_cgpclientmettype_w=>tt_clientmetadatatype(
    (
      VALUE /aws1/cl_cgpclientmettype_w=>ts_clientmetadatatype_maprow(
        key = |string|
        value = new /aws1/cl_cgpclientmettype_w( |string| )
      )
    )
  )
  it_scopes = VALUE /aws1/cl_cgpscopelisttype_w=>tt_scopelisttype(
    ( new /aws1/cl_cgpscopelisttype_w( |string| ) )
  )
  iv_clientid = |string|
  iv_secret = |string|
).

This is an example of reading all possible response values

lo_result = lo_result.
IF lo_result IS NOT INITIAL.
  lo_clientauthenticationres = lo_result->get_clientauthntctnresult( ).
  IF lo_clientauthenticationres IS NOT INITIAL.
    lv_tokenmodeltype = lo_clientauthenticationres->get_accesstoken( ).
    lv_integertype = lo_clientauthenticationres->get_expiresin( ).
    lv_stringtype = lo_clientauthenticationres->get_tokentype( ).
  ENDIF.
ENDIF.

Example – Get an access token for machine-to-machine authorization

The following example gets an access token for the app client 1example23456789 with the custom scope solar-system-data/asteroids.add.

DATA(lo_result) = lo_client->getclienttoken(
  it_scopes = VALUE /aws1/cl_cgpscopelisttype_w=>tt_scopelisttype(
    ( new /aws1/cl_cgpscopelisttype_w( |solar-system-data/asteroids.add| ) )
  )
  iv_clientid = |1example23456789|
  iv_secret = |exampleClientSecret123EXAMPLE|
).