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.
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.
Parameters:
-
client_id
(ClientIdType)
–
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.
-
secret
(ClientSecretType)
–
An active secret for the app client.
-
scopes
(Optional[ScopeListType], default:
None
)
–
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.
-
client_metadata
(Optional[ClientMetadataType], default:
None
)
–
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.
Raises:
-
ForbiddenException
–
This exception is thrown when WAF doesn't allow your request based on a web ACL that's associated with your user pool.
-
InternalErrorException
–
This exception is thrown when Amazon Cognito encounters an internal error.
-
InvalidParameterException
–
This exception is thrown when the Amazon Cognito service encounters an invalid parameter.
-
NotAuthorizedException
–
This exception is thrown when a user isn't authorized.
-
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.
-
ResourceNotFoundException
–
This exception is thrown when the Amazon Cognito service can't find the requested resource.
-
TooManyRequestsException
–
This exception is thrown when the user has made too many requests for a given operation.
-
UnknownServiceError
–
The service returned an error code this client does not model.
Examples:
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.
>>> client.get_client_token(client_id='1example23456789', secret='exampleClientSecret123EXAMPLE', scopes=['solar-system-data/asteroids.add'])
Source code in docs_src/capo_cognito_identity_provider/_services/cognito_identity_provider.py
| def get_client_token(
self,
client_id: "capo_cognito_identity_provider.types.client_id_type.ClientIdType",
secret: "capo_cognito_identity_provider.types.client_secret_type.ClientSecretType",
*,
config_overrides: Optional[CognitoIdentityProviderClientConfig] = None,
scopes: Optional[
"capo_cognito_identity_provider.types.scope_list_type.ScopeListType"
] = None,
client_metadata: Optional[
"capo_cognito_identity_provider.types.client_metadata_type.ClientMetadataType"
] = None,
) -> "capo_cognito_identity_provider.types.get_client_token_response.GetClientTokenResponse":
"""<p>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.</p> <p>To use this operation, you must configure the app client with a client secret and enable the <code>ALLOW_CLIENT_TOKEN_AUTH</code> authentication flow. The <code>ALLOW_CLIENT_TOKEN_AUTH</code> 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 <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-define-resource-servers.html">Scopes, M2M, and resource servers</a>.</p> <note> <p>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 <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/user-pools-API-operations.html">Using the Amazon Cognito user pools API and user pool endpoints</a>.</p> </note>
Args:
client_id: <p>The ID of the app client that requests the access token. The app client must have a client secret and the <code>ALLOW_CLIENT_TOKEN_AUTH</code> authentication flow.</p>
secret: <p>An active secret for the app client.</p>
scopes: <p>The custom scopes to authorize in the access token, in the format <code>resource-server-identifier/scope-name</code>. 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.</p>
client_metadata: <p>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.</p> <p>When Amazon Cognito invokes any of these functions, it passes a JSON payload, which the function receives as input. This payload contains a <code>clientMetadata</code> attribute that provides the data that you assigned to the ClientMetadata parameter in your request. In your function code, you can process the <code>clientMetadata</code> value to enhance your workflow for your specific needs.</p> <p>To review the Lambda trigger types that Amazon Cognito invokes at runtime with API requests, see <a href="https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-working-with-lambda-triggers.html#lambda-triggers-by-event"> Connecting API actions to Lambda triggers</a> in the <i>Amazon Cognito Developer Guide</i>.</p> <note> <p>When you use the <code>ClientMetadata</code> parameter, note that Amazon Cognito won't do the following:</p> <ul> <li> <p>Store the <code>ClientMetadata</code> 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 <code>ClientMetadata</code> parameter serves no purpose.</p> </li> <li> <p>Validate the <code>ClientMetadata</code> value.</p> </li> <li> <p>Encrypt the <code>ClientMetadata</code> value. Don't send sensitive information in this parameter.</p> </li> </ul> </note>
Raises:
capo_cognito_identity_provider.errors.forbidden_exception.ForbiddenException: <p>This exception is thrown when WAF doesn't allow your request based on a web ACL that's associated with your user pool.</p>
capo_cognito_identity_provider.errors.internal_error_exception.InternalErrorException: <p>This exception is thrown when Amazon Cognito encounters an internal error.</p>
capo_cognito_identity_provider.errors.invalid_parameter_exception.InvalidParameterException: <p>This exception is thrown when the Amazon Cognito service encounters an invalid parameter.</p>
capo_cognito_identity_provider.errors.not_authorized_exception.NotAuthorizedException: <p>This exception is thrown when a user isn't authorized.</p>
capo_cognito_identity_provider.errors.operation_not_enabled_exception.OperationNotEnabledException: <p>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.</p>
capo_cognito_identity_provider.errors.resource_not_found_exception.ResourceNotFoundException: <p>This exception is thrown when the Amazon Cognito service can't find the requested resource.</p>
capo_cognito_identity_provider.errors.too_many_requests_exception.TooManyRequestsException: <p>This exception is thrown when the user has made too many requests for a given operation.</p>
capo_cognito_identity_provider.errors.UnknownServiceError: The service returned an error code this client does not model.
Examples:
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.
>>> client.get_client_token(client_id='1example23456789', secret='exampleClientSecret123EXAMPLE', scopes=['solar-system-data/asteroids.add'])
"""
def _handler(
req: "OperationRequest[capo_cognito_identity_provider.types.get_client_token_request.GetClientTokenRequest]",
) -> OperationResponse[
"capo_cognito_identity_provider.types.get_client_token_response.GetClientTokenResponse"
]:
import capo_cognito_identity_provider._operations.aws_cognito_identity_provider_service.get_client_token
output, http_response = (
capo_cognito_identity_provider._operations.aws_cognito_identity_provider_service.get_client_token.get_client_token(
req.options, req.input
)
)
return OperationResponse(output=output, response=http_response)
def _redact_input(
input_: "capo_cognito_identity_provider.types.get_client_token_request.GetClientTokenRequest",
) -> dict[str, Any]:
import capo_cognito_identity_provider.types.get_client_token_request
return capo_cognito_identity_provider.types.get_client_token_request.redact_sensitive(
input_
)
def _redact_output(
output: "capo_cognito_identity_provider.types.get_client_token_response.GetClientTokenResponse",
) -> dict[str, Any]:
import capo_cognito_identity_provider.types.get_client_token_response
return capo_cognito_identity_provider.types.get_client_token_response.redact_sensitive(
output
)
interceptors_, options_ = self.operation_options(
config_overrides,
OperationLogging(
"GetClientToken",
redact_input=_redact_input,
redact_output=_redact_output,
),
)
input_: capo_cognito_identity_provider.types.get_client_token_request.GetClientTokenRequest = {
"client_id": client_id,
"secret": secret,
}
if scopes is not None:
input_["scopes"] = scopes
if client_metadata is not None:
input_["client_metadata"] = client_metadata
response = execute_pipeline(
OperationRequest(input=input_, options=options_),
handler=_handler,
interceptors=list(interceptors_),
)
response.response.close()
return response.output
|