Amazon Cognito的用户池 - CredentialsError:配置中缺少凭据

时间:2016-05-16 17:33:24

标签: amazon-web-services amazon-cognito

我正在尝试使用this教程创建Web App身份验证。这是我写的代码 -

var app = {};

app.configureCognito = function(){
    AWSCognito.config.region = 'us-east-1';

    var poolData = {
        UserPoolId: 'userPoolId',
        ClientId: 'ClientId'
    };

    var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);
    var userData = {
                    UserName:'MyName',
                    Pool: userPool
    };

    var attributeList = [];

    var dataEmail = {
            Name: 'email',
            Value: 'mailme@mydomain.com'
    };

    var dataPhoneNumber = {
            Name: 'phone_number',
            Value: '+9112212212212'
    };

    var attributeEmail = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataEmail);
    var attributePhoneNumber = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserAttribute(dataPhoneNumber);

    attributeList.push(attributeEmail);
    attributeList.push(attributePhoneNumber);

    var cognitoUser;

    userPool.signUp('userName','password',attributeList,null,function(err,result){
            if(err){
                    console.log(err);
                    alert(err);
                    return;
            }
            cognitoUser = result.user;
            console.log('User Name is: '+cognitoUser);
    });

};

我收到的错误是“在配置中缺少凭据”,我知道我们在这里没有做任何AWSCognito.config.credentials分配,但是它不应该使用userPool对象中提供的信息吗?代码有什么问题吗?根据UserPoolId和客户端ID,两者都是100%正确的。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:12)

我明确地使用下面的代码解决了这个问题,根本没有必要在这个例子中提供IdentityPoolId,这些只是占位符,可以留下如下 -

AWS.config.region = 'us-east-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: '...'
});

AWSCognito.config.region = 'us-east-1';
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: '...' 
});

需要设置凭证AWS.config.credentials和AWSCognito.config.credentials。完成上述步骤后,需要更新AWSCognito.config,如下所示 -

// Need to provide placeholder keys unless unauthorised user access is enabled for user pool
AWSCognito.config.update({accessKeyId: 'anything', secretAccessKey: 'anything'})

var poolData = { 
    UserPoolId : 'user pool id collected from user pool',
    ClientId : 'application client id of app subscribed to user pool'
};

dataPhoneNumber和userData是可选的,如果需要短信验证,则应提供dataPhoneNumber。

问题在上述问题得到解决后得到解决,Identity-Code是一个工作模式,如果有人想看看它。

答案 1 :(得分:1)

我使用AWS Amplify配置AWS SDK和AWS ses。我有同样的错误,但能够使用以下解决方案克服。我使用了AWS Amplify和React js。

希望这对那些对ampl和sdk有问题的人有所帮助。

Auth.currentCredentials().then(res => {

            AWS.config.update({
                region: 'ap-southeast-1',
                credentials: res
            });

            AWS.config.getCredentials(function (err) {
                if (err) console.log(err.stack); // credentials not loaded
                else console.log("Access Key:", AWS.config.credentials.accessKeyId);
            })


            var ses = new AWS.SES({
                region: 'ap-south-1',
                apiVersion: '2010-12-01'
            });

            this.setState({
                open: false,
                ses: ses
            });

            AWS.config.getCredentials(function (err) {
                if (err) console.log(err.stack); // credentials not loaded
                else console.log("Access Key:", AWS.config.credentials.accessKeyId);
            })

        })