【问题标题】:Testing AWS API Gateway with cURL使用 cURL 测试 AWS API Gateway
【发布时间】:2020-01-27 14:13:27
【问题描述】:

我确实有一个受 AWS_IAM 授权保护的简单 AWS API Gateway 实施。

我只想通过 cURL 从命令行进行测试:

curl --location --request GET 'https://<API_ID>.execute-api.eu-west-1.amazonaws.com/stage?type=type&category=category&lc=lc&passprhase=passprhase&product=product'
--header 'Authorization: AWS4-HMAC-SHA256 Credential=<AWS_ACCESS_KEY>/20200127/eu-west-1/execute-api/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date, Signature=<AWS_SECRET_ACCESS_KEY>' --header 'Content-Type: application/json' \
--data-raw '{"query":"","variables":{}}'

但不断收到以下错误:

Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header.

有人可以建议我做错了什么吗?

【问题讨论】:

    标签: linux curl aws-api-gateway


    【解决方案1】:

    AWS_IAM 授权使用Sigv4,其计算过程需要某些标头的值 - 日期是其中之一。您将 x-amz-date 作为“SignedHeaders”字段的一部分传递,但实际上并未将其与其他标头一起传递。

    创建正确的 curl 命令以使用 AWS_IAM 调用 API 的一种方法是使用 Postman application。添加 API URL 并在“授权”选项卡下选择“AWS 签名”。然后,您可以选择“代码”选项并获取看起来像这样的完整 curl 命令 -

    curl -X POST \
      https://$API-ID.execute-api.$AWS_REGION.amazonaws.com/$STAGE/$RESOURCE \
      -H 'authorization: AWS4-HMAC-SHA256 Credential=$ACCESS_KEY/20200128/$AWS_REGION/execute-api/aws4_request, SignedHeaders=content-type;host;x-amz-date, Signature=$SIGNATURE_VALUE' \
      -H 'cache-control: no-cache' \
      -H 'content-type: application/x-www-form-urlencoded' \
      -H 'host: API-ID.execute-api.$AWS_REGION.amazonaws.com' \
      -H 'postman-token: 15f9498e-95b7-f22b-eed9-016cdea07424' \
      -H 'x-amz-date: $DATE_STAMP'
    

    Create a Canonical Request for Signature Version 4

    【讨论】:

    • 感谢您的回复,苏拉杰!在提供 AWS 签名凭据后,我实际上已经从 Postman (cURL) 复制/粘贴了命令.. 但它仍然不起作用。您是否可以显示/发送执行呼叫所需的步骤和命令?
    • @user161183 这个新的 curl 命令有日期标题吗?您是否尝试过从 Postman 访问相同的 URL? docs.aws.amazon.com/apigateway/latest/developerguide/…
    【解决方案2】:

    我建议使用awscurl,这样更容易。

    要安装awscurl,请单击here。有关文档,您可以参考here

    下面是调用apigateway调用lambda进行POST查询的示例。

    awscurl --service execute-api -X POST -d '{ "alias" : "xyx", "type" : "LDAP" }' https://.execute-api.us-west-2.amazonaws .com/Prod/user/groups/get --region us-west-2 --access_key ACCESS_KEY --secret_key mfBl0YJRsXDue4C5F5B6rz1eUpQpA8uC24RtSnsg --security_token SECURITY_TOKEN

    【讨论】:

      【解决方案3】:

      由于 curl 7.75 支持 V4 签名

      所以你应该可以使用

      curl --location --request GET 'https://$API-ID.execute-api.eu-west-1.amazonaws.com/stage?type=type&category=category&lc=lc&passprhase=passprhase&product=product'\
      --header 'Content-Type: application/json'  --user $ACCESS_KEY:$SECRET_KEY  --aws-sigv4 "aws:amz" \
      --data-raw '{"query":"","variables":{}}'
      

      您可以在此处找到更多文档:https://curl.se/libcurl/c/CURLOPT_AWS_SIGV4.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-25
        • 2017-05-29
        • 2017-12-07
        • 1970-01-01
        相关资源
        最近更新 更多