【问题标题】:User Pools for Amazon Cognito - CredentialsError: Missing credentials in configAmazon Cognito 的用户池 - CredentialsError: Missing credentials in config
【发布时间】:2016-09-12 14:11:50
【问题描述】:

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

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% 正确的。任何帮助将不胜感激。

【问题讨论】:

    标签: amazon-web-services amazon-cognito


    【解决方案1】:

    我显然通过使用下面的代码解决了这个问题,在这个例子中根本不需要提供 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 是一个可行的模型,如果有人想看看的话。

    【讨论】:

    • 哇,非常感谢!也解决了我的问题。这个文档太可怕了。您是从哪里找到这些信息的?
    • 这是一个不懈的努力,如果你想寻找参考,这里是完整的代码 - github.com/jeetendra-choudhary/Identity-Code.git
    • @ReluMesaros 谢谢老兄!
    【解决方案2】:

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

    希望这对遇到放大和 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);
                })
    
            })
    

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 2016-10-23
      • 2020-06-17
      • 2016-09-26
      • 2017-07-06
      • 2017-03-25
      • 2017-08-04
      • 2019-09-02
      • 1970-01-01
      相关资源
      最近更新 更多