HTTP POST 到 AWS 物联网

HTTP POST to AWS IoT

我想将 HTTP 设备连接到 IoT Core。

我已经尝试 this 使用 curl 命令一切顺利。

现在我想尝试将 POST 与 signatyure version 4

一起使用

我正在使用 postmand 发送一个 POST 请求,但我得到了这个输出:

"message": "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.",

在授权字段中,我选择了 "AWS Signature" 并且我已经完成了所有这些:访问和密钥、aws 区域和服务名称=iotdata

我想获得与使用 curl 命令时相同的结果:

curl --tlsv1.2 --cacert YY.pem --cert XX.pem.crt --key ZZ.pem.key -X POST -d "{ \"Trama\": \"message\"}" "https://PPPPPP.iot.eu-west-1.amazonaws.com:8443/topics/topicname?qos=1"

这里的问题是您的两个命令略有不同,因为您可以通过多种方式将数据发送到 AWS IoT。

curl 命令中,您实际上使用的是 x.509 证书方法(您可以在此处查看更多信息:https://docs.aws.amazon.com/iot/latest/developerguide/managing-device-certs.html)这不需要对有效负载进行签名,它是已经信任,因为证书是。

这种方法主要是 AWS IoT 独有的,因为其目的是数据来自大量设备,而您不希望为它们全部赋予 IAM 角色。事实上,证书是从设备发送数据的推荐方式。

如果需要,您 可以 将这些证书与 Postman 一起使用,方法是将它们添加到证书选项卡下的请求中(您只需要 .crt.key 文件)。有关详细说明,请参阅 https://www.getpostman.com/docs/v6/postman/sending_api_requests/certificates

您仍然可以使用 AWS v4 签名 (https://docs.aws.amazon.com/iot/latest/developerguide/iam-users-groups-roles.html),所以建议您没有正确形成请求。

查看此文档 (https://docs.aws.amazon.com/iot/latest/apireference/API_iotdata_Publish.html),您应该使用:

方法POST

Uri<AWS IoT Endpoint>/<url_encoded_topic_name>?qos=1(例如https://a1pn10j0v8htvw.iot.us-east-1.amazonaws.com:8443/topics/iotbutton/virtualButton?qos=1

授权类型:AWS 签名

AccessKey / SecretKey:根据您的凭据

AWS 区域:您的 AWS IoT 实例所在的区域

服务名称iotdata

会话令牌:留空