1

我正在使用 AWS 为 WebApp 工作。我正在尝试从我的 DynamoDB 表中获取项目,但我收到错误“此身份池不支持未经身份验证的访问”。我不希望我的应用程序拥有未经身份验证的用户,但我在调用 DynamoDB 查询之前已登录。谁能帮我?这是我的代码:

function facebookLogin () {

FB.login(function (response) {
  if (response.authResponse) { // logged in
    AWS.config.credentials = new AWS.CognitoIdentityCredentials({
        IdentityPoolId: 'myActualPoolId'
    });

    AWS.config.region = 'us-east-1';
    AWS.config.credentials.params.logins = {}
    AWS.config.credentials.params.logins['graph.facebook.com'] = response.authResponse.accessToken;
    AWS.config.credentials.expired = true;



    console.log("Importing drivers into DynamoDB. Please wait.");


    var drivers = JSON.parse('[{"userId": "4","driverId": "4d","ratingValue": 3,"truckId": "4"},{"userId": "5","driverId": "5d","ratingValue": 2,"truckId": "5"}]');
    drivers.forEach(function(driver) {
        var params = {
            TableName: "myActualTableName",
            Item: {
                "userId":  driver.year,
                "driverId": driver.title,
                "ratingValue":  driver.info,
                "truckId": driver.truckId
            }
        };

        var docClient = new AWS.DynamoDB.DocumentClient();
        docClient.put(params, function(err, data) {
           if (err) {
               console.error("Unable to add driver", driver.userId, ". Error JSON:", JSON.stringify(err, null, 2));
           } else {
               console.log("PutItem succeeded:", driver.userId);
           }
        });
    });

  } else {
    console.log('There was a problem logging you in.');
  }
  });
 }

我将不胜感激任何帮助。谢谢!

4

1 回答 1

2

你很亲密。Cognito 凭据提供程序会延迟获取凭据,因此在设置登录时,您不会调用将登录链接到身份,因此对 Dynamo 的调用是使用未经身份验证的 id 进行的。Cognito开发指南有具体示例说明如何执行此操作,相关示例如下:

FB.login(function (response) {

// Check if the user logged in successfully.
if (response.authResponse) {

console.log('You are now logged in.');

// Add the Facebook access token to the Cognito credentials login map.
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'IDENTITY_POOL_ID',
  Logins: {
    'graph.facebook.com': response.authResponse.accessToken
  }
});

// Obtain AWS credentials
AWS.config.credentials.get(function(){
    // Access AWS resources here.
});

} else {
  console.log('There was a problem logging you in.');
}

});
于 2016-05-13T20:52:02.037 回答