# Sample code to demonstrate how to integrate an identity provider with an identity pool can be structured as follows:import boto3# Initialize the Amazon Cognito Identity clientclient = boto3.client('cognito-identity')# Assume you have already created an identity pool and obtained the IdentityPoolIdidentity_pool_id ='your-identity-pool-id'# Add an identity provider to the identity poolresponse = client.set_identity_pool_roles(IdentityPoolId=identity_pool_id,Roles={'authenticated': 'arn:aws:iam::AWS_ACCOUNT_ID:role/AuthenticatedRole','unauthenticated': 'arn:aws:iam::AWS_ACCOUNT_ID:role/UnauthenticatedRole',})# Print the response from AWSprint(response)
Cognito Sync
要生成身份池会话,您首先需要生成身份 ID。这个身份 ID 是该用户会话的标识。这些标识可以有多达 20 个数据集,可以存储多达 1MB 的键值对。
# Get auth IDawscognito-identityget-id--identity-pool-id<identity_pool_id>--no-sign# Get login tokenawscognito-identityget-open-id-token--identity-id<identity_id>--no-sign# Use login token to get IAM session creds## If you don't know the role_arn use the previous enhanced flow to get itaws sts assume-role-with-web-identity --role-arn "arn:aws:iam::<acc_id>:role/<role_name>" --role-session-name sessionname --web-identity-token <token> --no-sign
如果您收到此错误,则是因为基本流程未启用(默认)
An error occurred (InvalidParameterException) when calling the GetOpenIdToken operation: Basic (classic) flow is not enabled, please use enhanced flow.