简体   繁体   English

无法使用来自 AWS 的 CognitoIdentityServiceProvider SDK

[英]Unable to use CognitoIdentityServiceProvider from AWS SDK

I'm currently using amazon-cognito-identity-js and CognitoIdentityServiceProvider我目前正在使用amazon-cognito-identity-jsCognitoIdentityServiceProvider

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 时,我正在使用accessKeyIdsecretAccessKey初始化this.cognitoProvider

Is there a way I can use the CognitoIdentityServiceProvider without specifying accessKeyId and secretAccessKey ?有没有一种方法可以在不指定accessKeyIdsecretAccessKey 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.

相关问题 如何用 Jest 模拟 AWS Cognito CognitoIdentityServiceProvider? - How to mock AWS Cognito CognitoIdentityServiceProvider with Jest? Java v2 的 AWS SDK 无法从 EC2 实例加载凭证 - AWS SDK for Java v2 is unable to load credentials from EC2 instance 如何使用 aws java sdk 中的 getExecutionHistory 检查步骤 function 的状态 - How to use getExecutionHistory from aws java sdk to check status of a step function 如何在 AEM 6.5 maven 项目中使用 OSGi 包中的 AWS SDK - How to use the AWS SDK from an OSGi bundle in AEM 6.5 maven project AWS SDK 无法通过区域提供商链从 docker 容器中找到区域 - AWS SDK Unable to find a region via the region provider chain from docker container NodeJS AWS SDK 无法从 EC2 实例对附加了实例配置文件的 SQS 进行 API 调用 - NodeJS AWS SDK unable to make API calls from EC2 Instance to SQS with Instance Profile Attached AWS DotNet SDK 错误:无法从 EC2 实例元数据服务获取 IAM 安全凭证 - AWS DotNet SDK Error: Unable to get IAM security credentials from EC2 Instance Metadata Service AWS Java SDK - 无法通过区域提供商链找到区域 - AWS Java SDK - Unable to find a region via the region provider chain 无法删除 AWS 证书(证书正在使用中) - Unable to delete AWS certificate (Certificate is in use) 无法从 ~/.aws/config 和 ~/.aws/credentials 文件加载 AWS 凭证 - Unable to load AWS credentials from ~/.aws/config and ~/.aws/credentials file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM