什么是 Cognito IdentityId?

197*_*977 6 amazon-web-services aws-cognito

我正在尝试为我刚刚创建的用户获取 AWS 凭证。

谁能告诉我identityId应该是什么?我曾尝试将区域与用户 sub 连接,但它没有:

var params = {
  UserPoolId: process.env.USER_POOL_ID,
  Username: 'xxx@gmail.com',
  TemporaryPassword: 'Passw0rd!'
};

var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
cognitoidentityserviceprovider.adminCreateUser(params, function(err, data) {
  if (err) {
    callback(null, failure(err));  
  }else    
    var identityId =  "us-east-1:" + data.User.Username  //user sub

    var cognitoidentity = new AWS.CognitoIdentity();
    cognitoidentity.getCredentialsForIdentity(
      {"IdentityId": identityId},
      (err, credResult) => {
        if(err){
          callback(null, failure(err));
        }
        callback(null, success(credResult));
    })
});
Run Code Online (Sandbox Code Playgroud)

我只是得到:

{
  "message":"Identity 'us-east-1:8ce7ee63-d9ae-4f12-9xxxxxx' not found.", 
  "code":"ResourceNotFoundException","t": "..."
}
Run Code Online (Sandbox Code Playgroud)

deg*_*ege 8

您似乎将 Cognito 用户池与 Cognito 联合身份混合在一起。Cognito 用户池是您管理用户的地方,而联合身份是您授予外部用户 AWS 凭证访问权限的地方。

也就是说,您必须确保您的身份池(来自联合身份)配置为从您的用户池中访问用户。这可能会帮助您解决https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-integrating-user-pools-with-identity-pools.html

设置好之后,您可以调用CognitoIdentity.getId,它只会在您获得凭据之后才提供您的 IdentityId。

总结一下:IdentityId 是来自 Cognito Federated Identities 的身份池中用户的 ID。