如何通过 HTTPS 将事件发送到 Azure IoT 中心
How do I send an event to Azure IoT Hub via HTTPS
我在 Azure 上设置了一个 IoT 中心,并创建了一个名为 "ServerTemp" 的设备。我生成了一个 SAS 令牌,它似乎被接受了(我没有收到 401)。但是我收到了 400 Bad Request。
这是我通过 curl 发送的请求:
curl -v -H"Authorization:SharedAccessSignature sr=A794683.azure-devices.net&sig=<snip>" -H"Content-Type:application/json" -d'{"deviceId":"ServerTemp","temperature":70}' https://A479683.azure-devices.net/devices/ServerTemp/messages/events?api-version=2016-11-14
请求和响应(curl 的输出):
> POST /devices/ServerTemp/messages/events?api-version=2016-11-14 HTTP/1.1
> Host: A479683.azure-devices.net
> User-Agent: curl/7.47.0
> Accept: */*
> Authorization:SharedAccessSignature sr=A794683.azure-devices.net&sig=<snip>
> Content-Type:application/json
> Content-Length: 42
>
* upload completely sent off: 42 out of 42 bytes
< HTTP/1.1 400 Bad Request
< Content-Length: 151
< Content-Type: application/json; charset=utf-8
< Server: Microsoft-HTTPAPI/2.0
< iothub-errorcode: ArgumentInvalid
< Date: Sun, 15 Apr 2018 22:21:50 GMT
<
* Connection #0 to host A479683.azure-devices.net left intact
{"Message":"ErrorCode:ArgumentInvalid;BadRequest","ExceptionMessage":"Tracking ID:963189cb515345e69f94300655d3ca23-G:10-TimeStamp:04/15/2018 22:21:50"}
我做错了什么?
确保在创建 SAS 时添加到期时间 &se=
(如 &se=1555372034
)。它应该是最后一个参数。这是我可以重现您看到的 HTTP 400 的唯一方法(通过省略它)。一旦你解决了这个问题,你应该得到一个 204 No Content
。
资源 (&sr=
) 部分在您的情况下似乎也有点不对劲,没有指定设备。使用 Device Explorer 生成一个 device SAS(或者只是看看它应该是什么样子):Management > SAS Token.
SAS structure —
SharedAccessSignature sig={signature-string}&se={expiry}&skn={policyName}&sr={URL-encoded-resourceURI}
$ curl -i https://poorlyfundedskynet.azure-devices.net/devices/dexter/messages/events?api-version=2016-11-14 \
-H "Authorization: SharedAccessSignature sr=poorlyfundedskynet.azure-devices.net%2fdevices%2fdexter&sig=RxxxxxxxtE%3d&se=1555372034" \
-H "Content-Type: application/json" \
-d'{"deviceId":"dexter","temperature":70}'
HTTP/1.1 204 No Content
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
Date: Sun, 15 Apr 2018 23:54:25 GMT
您可以使用 Device Explorer 或 iothub-explorer 监控入口:
我在 Azure 上设置了一个 IoT 中心,并创建了一个名为 "ServerTemp" 的设备。我生成了一个 SAS 令牌,它似乎被接受了(我没有收到 401)。但是我收到了 400 Bad Request。
这是我通过 curl 发送的请求:
curl -v -H"Authorization:SharedAccessSignature sr=A794683.azure-devices.net&sig=<snip>" -H"Content-Type:application/json" -d'{"deviceId":"ServerTemp","temperature":70}' https://A479683.azure-devices.net/devices/ServerTemp/messages/events?api-version=2016-11-14
请求和响应(curl 的输出):
> POST /devices/ServerTemp/messages/events?api-version=2016-11-14 HTTP/1.1
> Host: A479683.azure-devices.net
> User-Agent: curl/7.47.0
> Accept: */*
> Authorization:SharedAccessSignature sr=A794683.azure-devices.net&sig=<snip>
> Content-Type:application/json
> Content-Length: 42
>
* upload completely sent off: 42 out of 42 bytes
< HTTP/1.1 400 Bad Request
< Content-Length: 151
< Content-Type: application/json; charset=utf-8
< Server: Microsoft-HTTPAPI/2.0
< iothub-errorcode: ArgumentInvalid
< Date: Sun, 15 Apr 2018 22:21:50 GMT
<
* Connection #0 to host A479683.azure-devices.net left intact
{"Message":"ErrorCode:ArgumentInvalid;BadRequest","ExceptionMessage":"Tracking ID:963189cb515345e69f94300655d3ca23-G:10-TimeStamp:04/15/2018 22:21:50"}
我做错了什么?
确保在创建 SAS 时添加到期时间 &se=
(如 &se=1555372034
)。它应该是最后一个参数。这是我可以重现您看到的 HTTP 400 的唯一方法(通过省略它)。一旦你解决了这个问题,你应该得到一个 204 No Content
。
资源 (&sr=
) 部分在您的情况下似乎也有点不对劲,没有指定设备。使用 Device Explorer 生成一个 device SAS(或者只是看看它应该是什么样子):Management > SAS Token.
SAS structure —
SharedAccessSignature sig={signature-string}&se={expiry}&skn={policyName}&sr={URL-encoded-resourceURI}
$ curl -i https://poorlyfundedskynet.azure-devices.net/devices/dexter/messages/events?api-version=2016-11-14 \
-H "Authorization: SharedAccessSignature sr=poorlyfundedskynet.azure-devices.net%2fdevices%2fdexter&sig=RxxxxxxxtE%3d&se=1555372034" \
-H "Content-Type: application/json" \
-d'{"deviceId":"dexter","temperature":70}'
HTTP/1.1 204 No Content
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
Date: Sun, 15 Apr 2018 23:54:25 GMT
您可以使用 Device Explorer 或 iothub-explorer 监控入口: