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.
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.
Note
Amazon Cognito doesn't evaluate AWS 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.
Request Syntax
{
"ClientId": "string",
"ClientMetadata": {
"string" : "string"
},
"Scopes": [ "string" ],
"Secret": "string"
}
Request Parameters
For information about the parameters that are common to all actions, see Common Parameters.
The request accepts the following data in JSON format.
- ClientId
-
The ID of the app client that requests the access token. The app client must have a client secret and the
ALLOW_CLIENT_TOKEN_AUTHauthentication flow.Type: String
Length Constraints: Minimum length of 1. Maximum length of 128.
Pattern:
[\w+]+Required: Yes
- ClientMetadata
-
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 AWS 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
clientMetadataattribute that provides the data that you assigned to the ClientMetadata parameter in your request. In your function code, you can process theclientMetadatavalue 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.
Note
When you use the
ClientMetadataparameter, note that Amazon Cognito won't do the following:-
Store the
ClientMetadatavalue. This data is available only to AWS Lambda triggers that are assigned to a user pool to support custom workflows. If your user pool configuration doesn't include triggers, theClientMetadataparameter serves no purpose. -
Validate the
ClientMetadatavalue. -
Encrypt the
ClientMetadatavalue. Don't send sensitive information in this parameter.
Type: String to string map
Key Length Constraints: Minimum length of 0. Maximum length of 131072.
Value Length Constraints: Minimum length of 0. Maximum length of 131072.
Required: No
-
- Scopes
-
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.Type: Array of strings
Array Members: Maximum number of 50 items.
Length Constraints: Minimum length of 1. Maximum length of 256.
Pattern:
[\x21\x23-\x5B\x5D-\x7E]+Required: No
- Secret
-
An active secret for the app client.
Type: String
Length Constraints: Minimum length of 24. Maximum length of 64.
Pattern:
[\w+]+Required: Yes
Response Syntax
{
"ClientAuthenticationResult": {
"AccessToken": "string",
"ExpiresIn": number,
"TokenType": "string"
}
}
Response Elements
If the action is successful, the service sends back an HTTP 200 response.
The following data is returned in JSON format by the service.
- ClientAuthenticationResult
-
The access token that Amazon Cognito issues for the app client, and its metadata.
Type: ClientAuthenticationResultType object
Errors
For information about the errors that are common to all actions, see Common Error Types.
- ForbiddenException
-
This exception is thrown when AWS WAF doesn't allow your request based on a web ACL that's associated with your user pool.
- message
-
The message returned when AWS WAF doesn't allow your request based on a web ACL that's associated with your user pool.
HTTP Status Code: 400
- InternalErrorException
-
This exception is thrown when Amazon Cognito encounters an internal error.
- message
-
The message returned when Amazon Cognito throws an internal error exception.
HTTP Status Code: 500
- InvalidParameterException
-
This exception is thrown when the Amazon Cognito service encounters an invalid parameter.
- message
-
The message returned when the Amazon Cognito service throws an invalid parameter exception.
- reasonCode
-
The reason code of the exception.
HTTP Status Code: 400
- NotAuthorizedException
-
This exception is thrown when a user isn't authorized.
- message
-
The message returned when the Amazon Cognito service returns a not authorized exception.
HTTP Status Code: 400
- OperationNotEnabledException
-
This exception is thrown when an operation is not available in the current region or for the current user pool configuration. This can occur when attempting to perform operations that are not supported in secondary replica regions.
HTTP Status Code: 400
- ResourceNotFoundException
-
This exception is thrown when the Amazon Cognito service can't find the requested resource.
- message
-
The message returned when the Amazon Cognito service returns a resource not found exception.
HTTP Status Code: 400
- TooManyRequestsException
-
This exception is thrown when the user has made too many requests for a given operation.
- message
-
The message returned when the Amazon Cognito service returns a too many requests exception.
HTTP Status Code: 400
See Also
For more information about using this API in one of the language-specific AWS SDKs, see the following: