使用 Cognito 签署 AWS 请求 Your User Pool user using Postman

Sign AWS requests using Cognito Your User Pool user using Postman

我正在尝试向 AWS API 网关发送经过身份验证的请求。客户端将针对 Cognito Your User Pools 进行身份验证,然后从关联的 Cognito Identity Pool 中获取与用户池中登录用户相对应的令牌。我正在尝试使用 Postman 模拟这样的请求。

This post建议可以使用命令aws cognito-identity get-credentials-for-identity获取Postman签名请求所需的AccessKeyId和SecretKey。但是,当我尝试使用来自 Cognito 用户池控制台的用户的 sub 属性 运行 时:

$ aws cognito-identity get-credentials-for-identity --identity-id aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee

An error occurred (ValidationException) when calling the GetCredentialsForIdentity operation: 1 validation error detected: Value 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' at 'identityId' failed to satisfy constraint: Member must satisfy regular expression pattern: [\w-]+:[0-9a-f-]+

$ aws cognito-identity get-credentials-for-identity --identity-id us-east-1:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee

An error occurred (ResourceNotFoundException) when calling the GetCredentialsForIdentity operation: Identity 'us-east-1:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' not found.

如果我尝试使用 AWS 控制台中显示的关联身份池中的身份 ID(我选择了一个具有 2 "linked logins" 的身份 ID),也会发生同样的事情。

API 网关现在与 'Cognito Your User Pool' 原生集成,因此您可以直接传递身份令牌 - api gateway docs。您链接的 post 已过时

您需要传递登录地图:

--logins (map)
A  set  of  optional  name-value  pairs  that  map provider names to
provider tokens.

Shorthand Syntax:

    KeyName1=string,KeyName2=string

JSON Syntax:

    {"string": "string"
        ...}

这个语法对我有用:

aws cognito-identity get-credentials-for-identity \
    --identity-id us-east-1:aaaa-bbb-ccc-bc54-rrrrrrr \
    --logins graph.facebook.com=kdajbdjkabkjbkjbkdbsckslcjxb

注意:--identity-id 不是身份池 ID,它是来自身份浏览器的身份。