使用Facebook登录,但仍然可以获得:“;此标识池不支持未经身份验证的访问“;

Login in with Facebook but still getting: "Unauthenticated access is not supported for this identity pool"

本文关键字:不支持 标识 访问 身份验证 登录 Facebook 使用      更新时间:2023-09-26

我正在为一个使用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.');
  }
  });
 }

如果有任何帮助,我将不胜感激。谢谢

你非常接近。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.');
}
});