View a markdown version of this page

Utiliser CreateIdentityPool avec un AWS SDK ou CLI - Amazon Cognito

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Utiliser CreateIdentityPool avec un AWS SDK ou CLI

Les exemples de code suivants illustrent comment utiliser CreateIdentityPool.

CLI
AWS CLI

Pour créer une réserve d’identités avec le fournisseur de réserves d’identités Cognito

Les suivants create-identity-pool l'exemple crée un pool d'identités nommé MyIdentityPool. Il dispose d’un fournisseur de réserve d’identités Cognito. Les identités non authentifiées ne sont pas autorisées.

aws cognito-identity create-identity-pool \ --identity-pool-name MyIdentityPool \ --no-allow-unauthenticated-identities \ --cognito-identity-providers ProviderName="cognito-idp.us-west-2.amazonaws.com/us-west-2_aaaaaaaaa",ClientId="3n4b5urk1ft4fl3mg5e62d9ado",ServerSideTokenCheck=false

Sortie :

{ "IdentityPoolId": "us-west-2:a1b2c3d4-5678-90ab-cdef-EXAMPLE11111", "IdentityPoolName": "MyIdentityPool", "AllowUnauthenticatedIdentities": false, "CognitoIdentityProviders": [ { "ProviderName": "cognito-idp.us-west-2.amazonaws.com/us-west-2_111111111", "ClientId": "3n4b5urk1ft4fl3mg5e62d9ado", "ServerSideTokenCheck": false } ] }

Pour plus d'informations, voir Démarrez avec les pools d'identités Amazon Cognito dans le Guide du développeur Amazon Cognito.

  • Pour plus de détails sur l'API, voir CreateIdentityPool dans AWS CLI Référence de commande.

Java
SDK pour Java 2.x
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

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 ""; } }
  • Pour plus de détails sur l'API, voir CreateIdentityPool dans AWS SDK for Java 2.x Référence de l'API.

PowerShell
Outils pour PowerShell V4

Exemple 1 : crée un nouveau groupe d’identités qui autorise les identités non authentifiées.

New-CGIIdentityPool -AllowUnauthenticatedIdentities $true -IdentityPoolName CommonTests13

Sortie :

LoggedAt : 8/12/2015 4:56:07 PM AllowUnauthenticatedIdentities : True DeveloperProviderName : IdentityPoolId : us-east-1:15d49393-ab16-431a-b26e-EXAMPLEGUID3 IdentityPoolName : CommonTests13 OpenIdConnectProviderARNs : {} SupportedLoginProviders : {} ResponseMetadata : Amazon.Runtime.ResponseMetadata ContentLength : 136 HttpStatusCode : OK
  • Pour plus de détails sur l'API, voir CreateIdentityPool dans Outils AWS pour PowerShell Référence de l'applet de commande (V4).

Outils pour PowerShell V5

Exemple 1 : crée un nouveau groupe d’identités qui autorise les identités non authentifiées.

New-CGIIdentityPool -AllowUnauthenticatedIdentities $true -IdentityPoolName CommonTests13

Sortie :

LoggedAt : 8/12/2015 4:56:07 PM AllowUnauthenticatedIdentities : True DeveloperProviderName : IdentityPoolId : us-east-1:15d49393-ab16-431a-b26e-EXAMPLEGUID3 IdentityPoolName : CommonTests13 OpenIdConnectProviderARNs : {} SupportedLoginProviders : {} ResponseMetadata : Amazon.Runtime.ResponseMetadata ContentLength : 136 HttpStatusCode : OK
  • Pour plus de détails sur l'API, voir CreateIdentityPool dans Outils AWS pour PowerShell Référence de l'applet de commande (V5).

Swift
Kit SDK pour Swift
Note

Il y en a plus sur GitHub. Trouvez l’exemple complet et découvrez comment le configurer et l’exécuter dans le référentiel d’exemples de code AWS.

import AWSCognitoIdentity /// Create a new identity pool and return its ID. /// /// - Parameters: /// - name: The name to give the new identity pool. /// /// - Returns: A string containing the newly created pool's ID, or `nil` /// if an error occurred. /// func createIdentityPool(name: String) async throws -> String? { do { let cognitoInputCall = CreateIdentityPoolInput(developerProviderName: "com.exampleco.CognitoIdentityDemo", identityPoolName: name) let result = try await cognitoIdentityClient.createIdentityPool(input: cognitoInputCall) guard let poolId = result.identityPoolId else { return nil } return poolId } catch { print("ERROR: createIdentityPool:", dump(error)) throw error } }

Pour une liste complète des AWS Guides du développeur du SDK et exemples de code, voir L'utilisation de ce service avec un AWS Kit SDK. Cette rubrique comprend également des informations sur le démarrage et sur les versions précédentes du kit SDK.