【问题标题】:AWS-amplify Including the cognito Authorization header in the requestAWS-amplify 在请求中包含 cognito Authorization 标头
【发布时间】:2018-11-11 16:45:14
【问题描述】:

我创建了一个包含 Cognito 和云逻辑的 AWS 移动中心项目。在我的 API 网关中,我为授权者设置了 Cognito 用户池。我使用 React Native 作为我的客户端应用程序。如何将授权标头添加到我的 API 请求中。

const request = {
  body: {
    attr: value
  }
};

API.post(apiName, path, request)
  .then(response => {
  // Add your code here
    console.log(response);
  })
  .catch(error => {
    console.log(error);
  });
};

【问题讨论】:

    标签: react-native aws-lambda aws-sdk amazon-cognito aws-amplify


    【解决方案1】:

    默认情况下,aws-amplify 的 API 模块会尝试对请求进行 sig4 签名。如果您的授权人类型是AWS_IAM,那就太好了。

    这显然不是您在使用 Cognito 用户池授权器时想要的。在这种情况下,您需要在 Authorization 标头中传递 id_token,而不是 sig4 签名。

    今天,您确实可以传递Authorization 标头来放大,以及it will no longer overwrite it with the sig4 signature


    在您的情况下,您只需将 headers 对象添加到您的 request 对象。例如:

    async function callApi() {
    
        // You may have saved off the JWT somewhere when the user logged in.
        // If not, get the token from aws-amplify:
        const user = await Auth.currentAuthenticatedUser();
        const token = user.signInUserSession.idToken.jwtToken;
    
        const request = {
            body: {
                attr: "value"
            },
            headers: {
                Authorization: token
            }
        };
    
        var response = await API.post(apiName, path, request)
            .catch(error => {
                console.log(error);
            });
    
        document.getElementById('output-container').innerHTML = JSON.stringify(response);
    }
    

    使用aws-amplify 0.4.1 测试。

    【讨论】:

    • 我收到错误 304 IncompleteSignatureException
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-04
    • 2017-01-13
    • 1970-01-01
    • 2017-02-24
    • 2019-09-17
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多