Services or capabilities described in AWS documentation might vary by Region. To see the differences applicable to the AWS European Sovereign Cloud Region, see the AWS European Sovereign Cloud User Guide.Use GetClientToken with an AWS SDK
The following code example shows how to use GetClientToken.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in
context in the following code example:
- Python
-
- SDK for Python (Boto3)
-
def get_client_token(
self,
client_id: str,
client_secret: str,
scopes: Optional[list[str]] = None,
) -> dict[str, Any]:
"""
Obtains an M2M access token for an app client using its client ID
and secret, without end-user sign-in.
:param client_id: The ID of the app client.
:param client_secret: The client secret for the app client.
:param scopes: Optional list of scopes in the format
'resource-server-identifier/scope-name'. If omitted,
all scopes configured for the app client are authorized.
:return: A dictionary containing the ClientAuthenticationResult with
AccessToken, ExpiresIn, and TokenType.
"""
try:
params = dict()
params["ClientId"] = client_id
params["Secret"] = client_secret
if scopes is not None:
params["Scopes"] = scopes
response = self.cognito_idp_client.get_client_token(**params)
auth_result = response.get("ClientAuthenticationResult", dict())
logger.info(
"Obtained M2M access token for client %s. Token type: %s, expires in: %s seconds.",
client_id,
auth_result.get("TokenType", "Unknown"),
auth_result.get("ExpiresIn", 0),
)
return auth_result
except ClientError as err:
if err.response["Error"]["Code"] == "NotAuthorizedException":
logger.error(
"Not authorized for client %s. Verify that the client ID and "
"secret are correct, and that the ALLOW_CLIENT_TOKEN_AUTH flow "
"is enabled for the app client.",
client_id,
)
raise
For a complete list of AWS SDK developer guides and code examples, see
Using this service with an AWS SDK.
This topic also includes information about getting started and details about previous SDK versions.