【问题标题】:Unable to overcome InvalidLambdaResponseException for Java AWS Lambda Cognito custom message trigger无法克服 Java AWS Lambda Cognito 自定义消息触发器的 InvalidLambdaResponseException
【发布时间】:2018-06-17 05:06:26
【问题描述】:

每当我在 cognito 中创建新帐户时尝试使用自定义消息触发器时,我都会不断收到带有消息“无法识别的 lambda 输出”的 InvalidLambdaResponseException。 lambda 触发器是用 java 编写的,我在查找文档或如何在 java 中编写 cognito 触发器的示例时遇到了麻烦。我能找到的最好的是here,它使我能够弄清楚如何正确格式化输出消息,但仍然抛出异常。我通过查看 JavaScript 示例了解到,您必须返回接收到的事件或具有相同参数格式的对象,我试图这样做无济于事。您将在下面找到我收到的云手表记录的输入和输出。我做错了什么?

这是原始输入

{version=1, region=us-east-1, userPoolId=*****,
userName= *****, callerContext={awsSdkVersion=aws-sdk-nodejs-2.166.0, 
clientId=*****}, triggerSource=CustomMessage_SignUp,
request={userAttributes={custom:position_group=["All"],
sub=*****, email_verified=false, cognito:user_status=UNCONFIRMED, 
given_name=*****, custom:permissions=, custom:team_id=*****,
email=*****@gmail.com}, codeParameter={####},usernameParameter=null}, 
response={smsMessage=null, emailMessage=null, emailSubject=null}}

这是输出

{
    "version": 1,
    "triggerSource": "CustomMessage_SignUp",
    "region": "us-east-1",
    "userPoolId": "*****",
    "userName": "*****",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-nodejs-2.166.0",
        "clientId": "*****"
    },
    "request": {
        "userAttributes": {
            "userAttributes": {
                "custom:position_group": "[\"All\"]",
                "sub": "*****",
                "email_verified": "false",
                "cognito:user_status": "UNCONFIRMED",
                "given_name": "*****",
                "custom:permissions": "",
                "custom:team_id": "*****",
                "email": "*****@gmail.com"
            },
            "codeParameter": "{####}"
        }
    },
    "response": {
        "smsMessage": "{####} is your verification code.",
        "emailMessage": "TEST {####}",
        "emailSubject": "Verification"
        } 
}

加法 在阅读了这个post 之后,我尝试将我的处理程序方法切换到这个

public void handleRequest(InputStream input, OutputStream output, Context context) throws IOException {
        LambdaLogger logger = context.getLogger();
        JsonNode json = mapper.readTree(input);
        logger.log(mapper.writeValueAsString(json));
        ObjectNode jsonWithResponse = (ObjectNode) json;
        jsonWithResponse.with("response").put("smsMessage", "TEST {####}");
        jsonWithResponse.with("response").put("emailMessage", "TEST {####}");
        jsonWithResponse.with("response").put("emailSubject", "TEST subject");
        try (Writer w = new OutputStreamWriter(output, "UTF-8")) {
                w.write(mapper.writeValueAsString(jsonWithResponse));
        }
        logger.log(mapper.writeValueAsString(jsonWithResponse));
    }

之前把这个放在云端观看

{
    "version": "1",
    "region": "us-east-1",
    "userPoolId": "*****",
    "userName": "*****",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-nodejs-2.166.0",
        "clientId": "*****"
    },
    "triggerSource": "CustomMessage_SignUp",
    "request": {
        "userAttributes": {
            "custom:position_group": "[\"All\"]",
            "sub": "*****",
            "email_verified": "false",
            "cognito:user_status": "UNCONFIRMED",
            "given_name": "*****",
            "custom:permissions": "",
            "custom:team_id": "*****",
            "email": "*****@gmail.com"
        },
        "codeParameter": "{####}",
        "usernameParameter": null
    },
    "response": {
        "smsMessage": null,
        "emailMessage": null,
        "emailSubject": null
    }
}

然后在云端观看

{
    "version": "1",
    "region": "us-east-1",
    "userPoolId": "*****",
    "userName": "*****",
    "callerContext": {
        "awsSdkVersion": "aws-sdk-nodejs-2.166.0",
        "clientId": "*****"
    },
    "triggerSource": "CustomMessage_SignUp",
    "request": {
        "userAttributes": {
            "custom:position_group": "[\"All\"]",
            "sub": "*****",
            "email_verified": "false",
            "cognito:user_status": "UNCONFIRMED",
            "given_name": "*****",
            "custom:permissions": "",
            "custom:team_id": "*****",
            "email": "*****@gmail.com"
        },
        "codeParameter": "{####}",
        "usernameParameter": null
    },
    "response": {
        "smsMessage": "TEST  {####}",
        "emailMessage": "TEST {####}",
        "emailSubject": "TEST subject"
    }
}

【问题讨论】:

  • 你还有这个 InvalidLambdaResponseException 吗? (即您是否还需要一些帮助,或者它已经解决了?)
  • 我想通了,我在下面发布了我的解决方案。

标签: java amazon-web-services aws-lambda aws-sdk aws-cognito


【解决方案1】:

我的问题是由于我没有将{username} 键添加到我的电子邮件回复消息(smsMessage 和 emailMessage)中。确保添加密钥{####}{username} 解决了它。

【讨论】:

  • 如果您要添加 {####}(用于电子邮件),链接文本是什么?我的意思是我们有模板(在 aws 上),它表明我们必须在电子邮件的情况下包含 {##LinkText##},但不是 {####}。
  • 我没有使用链接进行验证,我正在向用户发送验证码。我们不想向用户显示他们的用户名,因为它是一个对他们没有任何意义的 UUID,因此我们将其隐藏在欢迎电子邮件中。 (我想使用验证链接,但当时没有关于如何使用的文档。你们已经更新了你的文档并将查看)
  • 那是我的问题。当我使用代码时它确实有效,但不适用于链接验证。医生没有帮助。因此,如果您设法以某种方式使用带有确认链接的自定义电子邮件 - 请更新我。
猜你喜欢
  • 1970-01-01
  • 2018-03-11
  • 2019-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-04-22
  • 1970-01-01
  • 2023-01-01
  • 2018-03-10
相关资源
最近更新 更多