# 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.