awsdocs / aws-doc-sdk-examples

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.
Apache License 2.0
9.64k stars 5.66k forks source link

[Enhancement]: GetAccessToken for Java #4415

Open luetobeaws opened 1 year ago

luetobeaws commented 1 year ago

Background story

We would like to use the GetAccess Token using Client Credentials from Lambda to authenticate with Amazon Cognito

What does this example accomplish?

The example will describe how to obtain a token from Cognito using Client Credentials.

Which AWS service(s)?

cognito

Which AWS SDKs or tools?

Are there existing code examples to leverage?

https://stackoverflow.com/questions/56954135/getting-amazon-cognito-access-token-in-java

Do you have any reference code?

final String clientId = "XXXXXXXXXXXXXXXXXXXXXXXXX";
final String region = "eu-west-1";
final String username = "USERNAME";
final String password = "PASSWORD";
AWSCognitoIdentityProvider cognitoClient = AWSCognitoIdentityProviderClientBuilder.standard()
            .withRegion(region)
            .build();
final Map<String, String> authParams = new HashMap<>();
authParams.put("USERNAME", username);
authParams.put("PASSWORD", password);
final InitiateAuthRequest authRequest = new InitiateAuthRequest();
authRequest.withAuthFlow(AuthFlowType.USER_SRP_AUTH)
            .withClientId(clientId)
            .withAuthParameters(authParams);
InitiateAuthResult result = cognitoClient.initiateAuth(authRequest);
github-actions[bot] commented 1 year ago

Marked stale by the Shirriff. Notifying @awsdocs/aws-sdk-docs-code-maintainers