如何使用 Cognito Id(+配置)调用 AWS API Gateway 端点?

2024-01-19

我想打电话给AWS API Gateway Endpoint受保护的是AWS_IAM使用generated JavaScript API SDK.

我有一个Cognito UserPool and a Cognito Identity Pool。两者均通过正确同步ClientId.

我用这个代码Sign in并得到Cognito Identity

AWS.config.region = 'us-east-1'; // Region
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here
});

AWSCognito.config.region = 'us-east-1';
AWSCognito.config.credentials = new AWS.CognitoIdentityCredentials({
  IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXXXXXX' // your identity pool id here
});

var poolData = {
  UserPoolId: 'us-east-1_XXXXXXXX',
  ClientId: 'XXXXXXXXXXXXXXXXXXXXXXXX'
};
var userPool = new AWSCognito.CognitoIdentityServiceProvider.CognitoUserPool(poolData);


var authenticationData = {
  Username: 'user',
  Password: '12345678',
};
var authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
var userData = {
  Username: 'user',
  Pool: userPool
};
var cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess: function (result) {
  console.log('access token + ' + result.getAccessToken().getJwtToken());

  AWS.config.credentials = new AWS.CognitoIdentityCredentials({
    IdentityPoolId: 'us-east-1:XXXXXXXXXXXXXXXXXXXX',
    IdentityId: AWS.config.credentials.identityId,
    Logins: {
      'cognito-idp.us-east-1.amazonaws.com/us-east-1_XXXXXX': result.idToken.jwtToken
    }
  });

  AWS.config.credentials.get(function (err) {
    // now I'm using authenticated credentials
    if(err)
    {
      console.log('error in autheticatig AWS'+err);
    }
    else
    {
      console.log(AWS.config.credentials.identityId);

    }
  });
  },

  onFailure: function (err) {
    alert(err);
  }

});

这一切都成功了,我有一个authorized Cognito Identity now.

现在我尝试调用API Gateway Endpoint执行Lambda Function它指向。

  var apigClient = apigClientFactory.newClient({
    accessKey: AWS.config.credentials.accessKeyId, //'ACCESS_KEY',
    secretKey: AWS.config.credentials.secretAccessKey, //'SECRET_KEY',
    sessionToken: AWS.config.credentials.sessionToken, // 'SESSION_TOKEN', //OPTIONAL: If you are using temporary credentials you must include the session token
    region: 'us-east-1' // OPTIONAL: The region where the API is deployed, by default this parameter is set to us-east-1
  });

  var params = {
    // This is where any modeled request parameters should be added.
    // The key is the parameter name, as it is defined in the API in API Gateway.
  };

  var body = {
    // This is where you define the body of the request,
    query: '{person {firstName lastName}}'
  };

  var additionalParams = {
    // If there are any unmodeled query parameters or headers that must be
    //   sent with the request, add them here.
    headers: {},
    queryParams: {}
  };

  apigClient.graphqlPost(params, body, additionalParams)
    .then(function (result) {
      // Add success callback code here.
      console.log(result);
    }).catch(function (result) {
    // Add error callback code here.
    console.log(result);
  });

但不幸的是这失败了。这OPTIONS请求成功200但是POST然后失败403.

我很确定没有CORS问题在这里。

我很确定这个问题与IAM Roles and AWS Resource Configurations.

我的问题基本上是,你能为我提供所有必要的信息吗?AWS Resource Configurations and IAM Roles请问这是必要的吗?

我拥有的资源有

  • API 网关 - 已部署 API 端点
  • Lambda 函数 - 由端点调用
  • Cognito 用户池 - 应用程序同步到身份池
  • Cognito 身份池 - 已映射授权和未授权角色。
  • IAM 角色 - 用于 Lambda 函数以及 Cognito 身份池的授权和未授权角色。

但我不知道如何正确配置这些资源才能使其正常工作。

谢谢


Cognito身份角色有哪些访问权限?确保它有权执行execute-api:Invoke在您的 API 上。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "execute-api:Invoke"           
      ],
      "Resource": [
        "arn:aws:execute-api:us-east-1:<account>:<rest-api>/*/POST/graphql"
      ]
    }
  ]
} 

您可以从 Web 控制台的方法设置页面获取准确的资源 ARN。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Cognito Id(+配置)调用 AWS API Gateway 端点? 的相关文章

随机推荐

  • 如何在WebSphere 8.5上安装Jenkins

    我正在尝试安装 jenkins 1 627 如上所述wiki https wiki jenkins ci org display JENKINS IBM WebSphere 虽然我收到了这个错误 无法加载 web 应用程序 SRVE8111
  • 我们需要用 c++11 重新编译库吗?

    这是一个非常无知的问题 但是 我想开始使用 C 11 我可以继续使用使用旧的 gcc 4 2 1 编译器编译的大量库 还是需要使用新编译器重新编译它们 我认为 或希望 答案是否定的 但我只是一个涉足者 这样我至少可以消除部分无知 你能解释一
  • Url 编码 # (%23) 导致 ASP.NET 应用程序中出现 404

    我有一个深层链接的 Silverlight RIA 试图使用 Twitter OAuth 回调 RIA 中回调 页面 的 URL 为 http example com RiaTestPage aspx callback 只要 符号经过 UR
  • IE9是否支持console.log,它是一个真正的功能吗?

    在什么情况下是window console log在 Internet Explorer 9 中定义 即使当window console log被定义为 window console log apply and window console
  • 在 Angular JS (1.x) 中验证分页表单

    我有一个使用带输入字段的表格的角度形式 用户可以添加和删除行 每个单元格的输入类型可以是文本 数字 日期等 如果表格太大 表格就会变慢 解决此问题的一种方法是对表进行分页 不幸的是 对表格进行分页是一个问题 因为我对输入字段进行了自定义验证
  • lfe 包裹去了哪里?我怎样才能找到类似的信息?

    我正在寻找这个的更新版本 https cran r project org web packages lfe index html https cran r project org web packages lfe index html包裹
  • 链接器命令失败,sdl

    我正在尝试编译我的第一个 SDL 程序 但它无法编译 顺便说一句 我猜这不应该是关于设置库 因为我认为我正确安装了库 这是我的命令 g main cpp o main I Library Frameworks SDL2 framework
  • 如何使用 gganimate 动画让 x 轴跨度移动?

    使用 R 我尝试使用 gganimate 制作一个基于 x 轴从左到右显示的折线图 我已经设法做到了这一点 但我还想做的是使scale x continuous limits c i 5 i 5 即在正在显示的点和窗口周围有一个窗口将继续前
  • 成员访问不完整类型“QScrollBar”[重复]

    这个问题在这里已经有答案了 QScrollArea scrollArea new QScrollArea this scrollArea gt verticalScrollBar gt width 我试图获取 QScrollArea 的 V
  • 如何在 OncreateView 中运行异步功能?

    我的应用程序有问题 首先 我使用以下命令制作了两个选项卡碎片这会膨胀一个activity 实现的选项卡工作正常 其次我已经展示了XAML right 但是 我现在需要异步运行一些东西 Fragment 中的 OnCreateView 我怎样
  • 万物皆对象是如何运作的?

    我了解背后的主要理论一切都是对象但我真的不明白它是如何在幕后实现的 功能 So foo 4 是相同的foo call 4 但是什么阻止了我做foo call call 4 foo是一个函数并且foo call 都是围绕函数的方法包装器 但是
  • 复制带有下一个和随机指针的链表,仅赋予链表读取权限

    我需要复制带有下一个和随机指针的链表 下 一个指针照常指向链表中的下一个元素 随机指针可能指向任何其他节点 甚至指向其自身 如果我不允许在任何时候修改给定的列表 而只给出列表的读取权限 该怎么办 优雅的解决方案 线性时间 恒定空间 创建节点
  • Microsoft Exchange 不会将 PHPmailer 生成的电子邮件呈现为 HTML

    这个问题已经困扰我好几个星期了 我有一个脚本 可以在 PHPmailer 的帮助下将带有 xls 附件的 html 电子邮件发送给多个收件人 它已经运行良好一年多了 最近 来自同一家公司的两个使用 Microsoft Exchange 作为
  • 如何设置svn仓库的权限?

    我在网络驱动器上创建了一个存储库svnadmin create repos 有没有办法设置用户对存储库的权限 如果是这种情况 如何设置这些权限 如果您需要通过以下方式管理访问svn 协议 嵌入授权 您所需要做的就是更改文件conf新创建的存
  • 是否可以将标准的纯 C 标头 #include 指令放入命名空间中? [复制]

    这个问题在这里已经有答案了 可能的重复 将 include 包装在命名空间块中是个好主意吗 https stackoverflow com questions 6670738 is it a good idea to wrap an inc
  • 如何使用 roxygen 包从 dplyr 导入管道运算符 %>%

    我想用我编写的一些函数构建一个包 现在我的问题是 我无法将管道运算符 gt 与 dplyr 一起使用 我用 roxygen2 创建包 如果我编写没有 gt 的 dplyr 命令 则一切正常 代码里面 import dplyr readr m
  • 如何传递函数参数的值并运行独立的 Google Apps 脚本?

    从文档来看 https developers google com apps script guides standalone https developers google com apps script guides standalon
  • 如何在 vim 中进行语法检查?

    这个问题已经以这样或那样的形式被问过十几次了 这让我大吃一惊 为什么没有一个人真正解决如何配置合成的 http www vim org scripts script php script id 2736 or jslint http www
  • jsf 表达式语言中的 null 检查

    请参阅此表达语言 styleClass obj validationErrorMap eq null obj validationErrorMap contains key highlight field highlight row 即使地
  • 如何使用 Cognito Id(+配置)调用 AWS API Gateway 端点?

    我想打电话给AWS API Gateway Endpoint受保护的是AWS IAM使用generated JavaScript API SDK 我有一个Cognito UserPool and a Cognito Identity Poo