【发布时间】: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