【问题标题】:How do I send an event to Azure IoT Hub via HTTPS如何通过 HTTPS 将事件发送到 Azure IoT 中心
【发布时间】:2018-04-15 22:32:23
【问题描述】:

我在 Azure 上设置了一个 IoT 中心,并创建了一个名为“ServerTemp”的设备。我生成了一个 SAS 令牌,它似乎被接受了(我没有得到 401)。但我收到了 400 错误请求。

这是我通过 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"}

我做错了什么?

【问题讨论】:

    标签: azure iot


    【解决方案1】:

    确保在您形成 SAS 时添加到期时间 &amp;se=(如 &amp;se=1555372034)。它应该是最后一个参数。这是我可以重现您看到的 HTTP 400 的唯一方法(通过省略它)。一旦你解决了这个问题,你应该得到一个204 No Content

    在您的情况下,资源 (&amp;sr=) 部分似乎也有点偏离,没有指定设备。使用Device Explorer 生成一个设备 SAS(或者只是看看它应该是什么样子):Management > SAS Token

    SAS structure
    SharedAccessSignature sig={signature-string}&amp;se={expiry}&amp;skn={policyName}&amp;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 Exploreriothub-explorer 监控入口:

    也许这也可以:Azure IoT Extension for Azure CLI 2.0

    【讨论】:

    • 谢谢!使用设备资源管理器生成 SAS 就可以了。我使用的是示例 python 脚本,我一定是做错了什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 2018-01-10
    • 2021-07-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多