AWS – nodejs SDK – CognitoIdentityServiceProvider.initiateAuth – CredentialsError:在configuration中缺less凭据
我试图通过使用AWS Cognito和用户池的API来validation身份validation。 我有一个通过AWS Cognito用户池创build的用户,并且正在尝试与用户一起login。
我得到的错误是CredentialsError: Missing credentials in config
。 特别是它告诉我,我需要一个IdentityPoolId
。 但我似乎无法在我的AWS控制台中find此IdentityPoolId
。 在哪里,我从我的用户池得到这个? 我看到的只是一个Pool ID和一个Pool ARN。
相关源代码:
var aws = require('aws-sdk'); aws.config.update({ region: 'us-east-1', credentials: new aws.CognitoIdentityCredentials({ IdentityPoolId: '???' }) }); var authUser = function(params, callback) { if (!params || !params.Email || !params._password) { callback(new Error('Invalid parameters.')); return false; } var cognito = new aws.CognitoIdentityServiceProvider(); var authParams = { AuthFlow: 'USER_SRP_AUTH', // not sure what this means... ClientId: conf.AWSConfig.ClientId, AuthParameters: { Username: params.Email, Password: params._password } }; cognito.initiateAuth(authParams, function(err, data) { if (err) { console.log('Error details: ' + util.inspect(err)); callback(err); return false; } callback(null, {success: true, data: data}); }); }
对于authParams
对象,我不确定AuthFlow
应该是什么。 看看http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#initiateAuth-property它看起来像USER_SRP_AUTH
是我应该使用的。
编辑:
我相信我可能已经find了我的IdentityPoolId
。 我查看了Federated Identities
Authentication Providers
部分,并在身份Authentication Providers
部分的相应User Pool
添加了Authentication Providers
池。
我将Cognito
的Authentication Provider
关联到通过inputUser Pool ID
的User Pool ID
和App Client ID
创build的用户池。 现在使用相同的代码使用Identity pool ID
我得到错误CredentialsError: Missing credentials in config
。 它说Unauthorized access is not supported for this identity pool
。 好的…我试图授权用户…我是否需要创build一个未经身份validation的angular色,以便用户可以进行身份validation时,他们没有身份validation? 这看起来很愚蠢,如果这是我需要做的。
编辑2:
我还应该注意到,我能够使用JavaScript SDK(不是nodejs)login并获取AccessToken
以及IdToken
和RefreshToken
。 我做了这个,不需要IdentityPoolId
。 我唯一需要的是一个UserPoolId
和一个ClientId
。
var authenticateUser = function(onSuccessCallback) { var authData = { Username: getUserName(), // gets username from an html text field Password: getPassword() // gets password from an html password field }; var authDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authData); var cognitoUser = getCognitoUser(); cognitoUser.authenticateUser(authDetails, { onSuccess: function(result) { console.log('access token: ' + result.getAccessToken().getJwtToken()); console.log('idToken: ' + result.idToken.jwtToken); console.log(result); if (onSuccessCallback && typeof(onSuccessCallback) == 'function') { onSuccessCallback(cognitoUser); } }, onFailure: function(err) { // UserNotConfirmedException: User is not confirmed. console.log('authError'); alert(err); } }); }
原来, initiateAuth
并不是我想要做的。 我也错过了一个叫做amazon-cognito-identity-js
的npm包。 安装后,我更新了我的代码,如下所示:
var authUser = function(params, callback) { if (!params || !params.Email || !params._password) { callback(new Error('Invalid parameters.')); return false; } var poolData = { UserPoolId: conf.AWSConfig.UserPoolId, ClientId: conf.AWSConfig.ClientId }; var userPool = new aws.CognitoIdentityServiceProvider.CognitoUserPool(poolData); var authData = { Username: params.Email, Password: params._password }; var authDetails = new aws.CognitoIdentityServiceProvider.AuthenticationDetails(authData); var userData = { Username: params.Email, Pool: userPool }; var cognitoUser = new aws.CognitoIdentityServiceProvider.CognitoUser(userData); cognitoUser.authenticateUser(authDetails, { onSuccess: function(result) { callback(null, {success: true, data: result}); }, onFailure: function(err) { console.log('authUser error: ' + util.inspect(err)); callback(err); } }); }
我现在已经成功得到了令牌的回应! 万岁!
- 如何确定对象是否存在AWS S3 Node.JS sdk
- 亚马逊SES电子邮件不再发送
- 如何保持AWS实例上持久的http-server打开
- 当我在Amazon S3中将Quiet设置为true以在deleteObjects()方法中启用安静模式时会发生什么?
- AWS Elastic Beanstalk:我是否必须使用完整的“/ node_modules”和“/ bower_components”来部署我的应用程序,并编译sass和其他依赖项?
- aws api网关,lambda和node express
- 如何使用Lambda和DynamoDB在date范围之间扫描?
- AWS EB Node.JSlogin到CloudWatch
- 在集群模式下在Elastic Beanstalk上运行pm2