Amazon Cognito Identity examples using SDK for Java 2.x - Amazon SDK for Java 2.x
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Amazon Cognito Identity examples using SDK for Java 2.x

The following code examples show you how to perform actions and implement common scenarios by using the Amazon SDK for Java 2.x with Amazon Cognito Identity.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios and cross-service examples.

Scenarios are code examples that show you how to accomplish a specific task by calling multiple functions within the same service.

Each example includes a link to GitHub, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use CreateIdentityPool.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cognitoidentity.CognitoIdentityClient; import software.amazon.awssdk.services.cognitoidentity.model.CreateIdentityPoolRequest; import software.amazon.awssdk.services.cognitoidentity.model.CreateIdentityPoolResponse; import software.amazon.awssdk.services.cognitoidentityprovider.model.CognitoIdentityProviderException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class CreateIdentityPool { public static void main(String[] args) { final String usage = """ Usage: <identityPoolName>\s Where: identityPoolName - The name to give your identity pool. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String identityPoolName = args[0]; CognitoIdentityClient cognitoClient = CognitoIdentityClient.builder() .region(Region.US_EAST_1) .build(); String identityPoolId = createIdPool(cognitoClient, identityPoolName); System.out.println("Unity pool ID " + identityPoolId); cognitoClient.close(); } public static String createIdPool(CognitoIdentityClient cognitoClient, String identityPoolName) { try { CreateIdentityPoolRequest poolRequest = CreateIdentityPoolRequest.builder() .allowUnauthenticatedIdentities(false) .identityPoolName(identityPoolName) .build(); CreateIdentityPoolResponse response = cognitoClient.createIdentityPool(poolRequest); return response.identityPoolId(); } catch (CognitoIdentityProviderException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; } }

The following code example shows how to use DeleteIdentityPool.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.awscore.exception.AwsServiceException; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cognitoidentity.CognitoIdentityClient; import software.amazon.awssdk.services.cognitoidentity.model.DeleteIdentityPoolRequest; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DeleteIdentityPool { public static void main(String[] args) { final String usage = """ Usage: <identityPoolId>\s Where: identityPoolId - The Id value of your identity pool. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String identityPoold = args[0]; CognitoIdentityClient cognitoIdClient = CognitoIdentityClient.builder() .region(Region.US_EAST_1) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); deleteIdPool(cognitoIdClient, identityPoold); cognitoIdClient.close(); } public static void deleteIdPool(CognitoIdentityClient cognitoIdClient, String identityPoold) { try { DeleteIdentityPoolRequest identityPoolRequest = DeleteIdentityPoolRequest.builder() .identityPoolId(identityPoold) .build(); cognitoIdClient.deleteIdentityPool(identityPoolRequest); System.out.println("Done"); } catch (AwsServiceException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

The following code example shows how to use GetCredentialsForIdentity.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cognitoidentity.CognitoIdentityClient; import software.amazon.awssdk.services.cognitoidentity.model.GetCredentialsForIdentityRequest; import software.amazon.awssdk.services.cognitoidentity.model.GetCredentialsForIdentityResponse; import software.amazon.awssdk.services.cognitoidentityprovider.model.CognitoIdentityProviderException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class GetIdentityCredentials { public static void main(String[] args) { final String usage = """ Usage: <identityId>\s Where: identityId - The Id of an existing identity in the format REGION:GUID. """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String identityId = args[0]; CognitoIdentityClient cognitoClient = CognitoIdentityClient.builder() .region(Region.US_EAST_1) .build(); getCredsForIdentity(cognitoClient, identityId); cognitoClient.close(); } public static void getCredsForIdentity(CognitoIdentityClient cognitoClient, String identityId) { try { GetCredentialsForIdentityRequest getCredentialsForIdentityRequest = GetCredentialsForIdentityRequest .builder() .identityId(identityId) .build(); GetCredentialsForIdentityResponse response = cognitoClient .getCredentialsForIdentity(getCredentialsForIdentityRequest); System.out.println( "Identity ID " + response.identityId() + ", Access key ID " + response.credentials().accessKeyId()); } catch (CognitoIdentityProviderException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

The following code example shows how to use ListIdentityPools.

SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.cognitoidentity.CognitoIdentityClient; import software.amazon.awssdk.services.cognitoidentity.model.ListIdentityPoolsRequest; import software.amazon.awssdk.services.cognitoidentity.model.ListIdentityPoolsResponse; import software.amazon.awssdk.services.cognitoidentityprovider.model.CognitoIdentityProviderException; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class ListIdentityPools { public static void main(String[] args) { CognitoIdentityClient cognitoClient = CognitoIdentityClient.builder() .region(Region.US_EAST_1) .build(); listIdPools(cognitoClient); cognitoClient.close(); } public static void listIdPools(CognitoIdentityClient cognitoClient) { try { ListIdentityPoolsRequest poolsRequest = ListIdentityPoolsRequest.builder() .maxResults(15) .build(); ListIdentityPoolsResponse response = cognitoClient.listIdentityPools(poolsRequest); response.identityPools().forEach(pool -> { System.out.println("Pool ID: " + pool.identityPoolId()); System.out.println("Pool name: " + pool.identityPoolName()); }); } catch (CognitoIdentityProviderException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }