[英]Unable to use CognitoIdentityServiceProvider from AWS SDK
I'm currently using amazon-cognito-identity-js
and CognitoIdentityServiceProvider
我目前正在使用
amazon-cognito-identity-js
和CognitoIdentityServiceProvider
and following this article https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html并关注这篇文章https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html
When calling listUsersInGroup
function I'm initializing the this.cognitoProvider
with accessKeyId
and secretAccessKey
调用
listUsersInGroup
function 时,我正在使用accessKeyId
和secretAccessKey
初始化this.cognitoProvider
Is there a way I can use the CognitoIdentityServiceProvider
without specifying accessKeyId
and secretAccessKey
?有没有一种方法可以在不指定
accessKeyId
和secretAccessKey
CognitoIdentityServiceProvider
I don't want to specify these keys since it contains sensitive information我不想指定这些密钥,因为它包含敏感信息
This works这行得通
import { Config, CognitoIdentityCredentials, CognitoIdentityServiceProvider } from "aws-sdk";
export default class CognitoAuth {
configure(config) {
if (typeof config !== 'object' || Array.isArray(config)) {
throw new Error('[CognitoAuth error] valid option object required')
}
this.userPool = new CognitoUserPool({
UserPoolId: config.IDENTITY_POOL_ID,
ClientId: config.CLIENT_ID
})
this.cognitoProvider = new CognitoIdentityServiceProvider({
region: config.REGION,
accessKeyId: config.ACCESS_KEY_ID,
secretAccessKey: config.SECRET_ACCESS_KEY
});
Config.region = config.REGION
Config.credentials = new CognitoIdentityCredentials({
IdentityPoolId: config.IDENTITY_POOL_ID
})
this.options = config
}
getUsersInGroup(context, cb) {
var params = {
GroupName: context.group,
UserPoolId: this.options.IDENTITY_POOL_ID
};
this.cognitoProvider.listUsersInGroup(params, (err, data) => {
if (err) console.log(err, err.stack)
else cb(null, data.Users)
})
}
}
This don't work这行不通
this.cognitoProvider = new AWS.CognitoIdentityServiceProvider({ apiVersion: '2016-04-18' })
but I'm getting error ConfigError: Missing region in config
但我收到错误
ConfigError: Missing region in config
As per your linked documentation page, calling the listUsersInGroup
requires developer credentials, so these must be provided somehow.根据您链接的文档页面,调用
listUsersInGroup
需要开发人员凭据,因此必须以某种方式提供这些凭据。
If you look at Setting credentials in Node.js , there are different ways to pass them, eg, if running this function on a Lambda (or on an EC2 instance), it will use the Lambda (or EC2 instance) role permissions to call the method and credentials never have to be passed.如果您查看Node.js 中的设置凭据,可以通过不同的方式传递它们,例如,如果在 Lambda(或 EC2 实例)上运行此 function,它将使用 Lambda(或 EC2 实例)角色权限来调用该方法并且无需传递凭据。 Other options are using environment variables (
AWS_ACCESS_KEY_ID
/ AWS_SECRET_ACCESS_KEY
) or shared credentials file.其他选项使用环境变量 (
AWS_ACCESS_KEY_ID
/ AWS_SECRET_ACCESS_KEY
) 或共享凭证文件。
However, your immediate problem seems to be regarding the region.但是,您的直接问题似乎与该地区有关。 While in the working block it is passed with
region: config.REGION,
, it is missing from the non working block.在工作块中它通过
region: config.REGION,
传递时,它在非工作块中丢失。 You can fix that by passing the region
parameter when instantiating CognitoIdentityServiceProvider:您可以通过在实例化 CognitoIdentityServiceProvider 时传递
region
参数来解决此问题:
this.cognitoProvider = new AWS.CognitoIdentityServiceProvider({
apiVersion: '2016-04-18',
region: 'us-east-1' // use your region
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.