从网页登录 Cognito 有效,我同时获得了访问 token 和 ID token 。现在我想在登录时运行 Lambda 函数并访问用户的一些数据,但这里失败了。
我收到 InvalidLambdaResponseException:无效的 lambda 触发器源
。
对于造成这种情况的原因有什么想法吗?
Java Lambda 代码如下:
public class LambdaFunctionHandler implements RequestHandler<CognitoEvent, CognitoEvent> {
@Override
public CognitoEvent handleRequest(CognitoEvent event, Context context)
{
context.getLogger().log("Input: " + event);
return event;
}
}
Javascript:
function loginCognito()
{
AWSCognito.config.region = 'us-east-1';
var authenticationData = {
Username : '***',
Password : '***',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var poolData = { UserPoolId : 'us-east-1*********',
ClientId : '*******************'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
var userData = {
Username : '***',
Pool : userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails,
{
onSuccess: function (result) {
/* ... */
},
onFailure: function(err) {
alert(err);
}
});
}
最佳答案
在不太了解您想要执行的操作的具体情况的情况下,根据您返回的错误,我相信triggerSource在以下值之一中没有值:
PreSignUp_SignUp、PostConfirmation_ConfirmSignUp、PostConfirmation_ConfirmForgotPassword、PreAuthentication_Authentication、 PostAuthentication_Authentication、CustomMessage_SignUp、CustomMessage_AdminCreateUser、CustomMessage_ResendCode、CustomMessage_ForgotPassword、CustomMessage_UpdateUserAttribute、CustomMessage_VerifyUserAttribute、CustomMessage_Authentication、DefineAuthChallenge_Authentication、CreateAuthChallenge_Authentication、VerifyAuthChallengeResponse_Authentication
现在这不起作用的原因是 CognitoEvent是 CognitoSync 的模板(示例)服务而不是您使用的 userPools。目前我们不提供输入事件的JAVA示例。
要使其工作,您需要有一个可以序列化以下 JSON 的输入对象
{
"version": 1,
"triggerSource": "PostAuthentication_Authentication",
"region": "<region>",
"userPoolId": "<userPoolId>",
"userName": "<userName>",
"callerContext": {
"awsSdk": "<calling aws sdk with version>",
"clientId": "<apps client id>",
...
},
"request": {
"userAttributes": {
"phone_number_verified": true,
"email_verified": true,
... //all custom attributes
}
},
"response": {}
};
关于java - AWS Java Lambda Cognito - 无效的 lambda 触发器源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46488567/