java - AWS Java Lambda Cognito - 无效的 lambda 触发器源

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

从网页登录 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

http://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-identity-pools-working-with-aws-lambda-triggers.html#cognito-user-pools-lambda-trigger-syntax-shared

现在这不起作用的原因是 CognitoEventCognitoSync 的模板(示例)服务而不是您使用的 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/

相关文章:

amazon-web-services - 告诉 Lambda@Edge 函数执行重定向的代码是什么?

amazon-web-services - AWS CLI 筛选器返回特定 AMI 名称的最新 AMI 的唯一列表

java - 如何在带有 lambda 表达式的 Java 8 中使用多个流和 .map 函数

c# - 这个 lambda 表达式可以变得更简单吗?

java - 在保存 Activity 中打开另一个共享首选项会替换旧的共享首选项

java - NotificationCompat - 如何添加没有图标的操作?

java - IntelliJ 将数组值对齐到右边距

java - 如何在 App Engine Java 中配置 IP 地址和端口?

python - 将 OGM 与 AWS Neptune 结合使用

node.js - 如何在AWS Elasticache中测试Redis的性能?