Cognito admin_initiate_auth 在创建新用户时响应异常用户不存在

Cognito admin_initiate_auth responds with exception User does not exist when creating a new user

我正在尝试从我的 ruby 后端服务器在 Cognito 用户池中创建一个新用户。使用此代码:

client = Aws::CognitoIdentityProvider::Client.new
response = client.admin_initiate_auth({
  auth_flow: 'ADMIN_NO_SRP_AUTH',
  auth_parameters: {
    'USERNAME': @user.email,
    'PASSWORD': '123456789'
  },
  client_id: ENV['AWS_COGNITO_CLIENT_ID'],
  user_pool_id: ENV['AWS_COGNITO_POOL_ID']
})

我得到的回复是 Aws::CognitoIdentityProvider::Errors::UserNotFoundException: User does not exist

我正在尝试遵循服务器身份验证流程 (https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-authentication-flow.html),据此我了解到我可以使用 admin_initiate_auth.

创建新用户

我是不是做错了什么?

谢谢

你用错了方法。 admin_initiate_auth 用于记录 in/authenticating 一个开启了 ADMIN_NO_SRP_AUTH 的用户。

您需要使用sign_up方法:

resp = client.sign_up({
  client_id: "ClientIdType", # required
  secret_hash: "SecretHashType",
  username: "UsernameType", # required
  password: "PasswordType", # required
  user_attributes: [
    {
      name: "AttributeNameType", # required
      value: "AttributeValueType",
    },
  ],
  validation_data: [
    {
      name: "AttributeNameType", # required
      value: "AttributeValueType",
    },
  ],
  analytics_metadata: {
    analytics_endpoint_id: "StringType",
  },
  user_context_data: {
    encoded_data: "StringType",
  },
})

您可以在 AWS Cognito IDP docs here.

中找到它