GCP IoTCore 不会使用网关和 HTTP 桥解析有效载荷

GCP IoTCore won't parse payload using Gateways and HTTP bridge

到目前为止采取的步骤

  1. 创建新的密钥对并将其用于即将创建的网关
  2. 创建一个网关,我们称之为'my_first_gateway'
  3. 创建一个新设备,我们称之为'gw_device_1'
  4. 将 gw_device_1 关联到 my_first_gateway

到目前为止工作正常。

现在我想使用 HTTP 桥使用 my_first_gateway 的私钥通过我的网关将 gw_device_1 的状态数据发送到 IoTCore,遵循本教程:https://cloud.google.com/iot/docs/how-tos/gateways/http-bridge#setting_device_state_through_the_gateway

观察 1:本教程中的 URL 格式似乎有误,'delegated_device_id':

末尾缺少双引号
curl -X POST -H 'authorization: Bearer GATEWAY_JWT' -H 'content-type: application/json' --data '{"binary_data": "DATA", "gateway_info": {"delegated_device_id: "device-id"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/{project-id}/locations/{cloud-region}/registries/{registry-id}/devices/{gateway-id}:setState'

当我现在替换所有占位符并将 "DATA" 替换为 "ewogICJhUHJvcCI6ICJhVmFsdWUiCn0" 时,我执行以下 curl(令牌显然不是真实的):

curl -X POST -H 'authorization: Bearer GW_JWT_TOKEN' -H 'content-type: application/json' --data '{"binary_data": "ewogICJhUHJvcCI6ICJhVmFsdWUiCn0=", "gateway_info": {"delegated_device_id": "gw_device_1"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/my_project_id/locations/europe-west1/registries/my_registry/devices/my_first_gateway:setState'

我收到此错误:

{
  "error": {
    "code": 400,
    "message": "Invalid JSON payload received. Unknown name \"binary_data\": Cannot find field.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.BadRequest",
        "fieldViolations": [
          {
            "description": "Invalid JSON payload received. Unknown name \"binary_data\": Cannot find field."
          }
        ]
      }
    ]
  }
}

有趣的是:还有一个'endpoint'用于向IoTCore发布事件。它具有相同的签名,但不是 'setState',而是以 'publishEvent' 结尾(参见:https://cloud.google.com/iot/docs/how-tos/gateways/http-bridge#publishing_the_devices_telemetry_events_through_the_gateway)。 使用此方法执行完全相同的请求效果很好:

curl -X POST -H 'authorization: Bearer GW_JWT_TOKEN' -H 'content-type: application/json' --data '{"binary_data": "ewogICJhUHJvcCI6ICJhVmFsdWUiCn0=", "gateway_info": {"delegated_device_id": "gw_device_1"}}' -H 'cache-control: no-cache' 'https://cloudiotdevice.googleapis.com/v1/projects/my_project_id/locations/europe-west1/registries/my_registry/devices/my_first_gateway:publishEvent'

我错过了什么吗? 任何帮助表示赞赏。

实际上,google提供的卷曲是不正确的。 负载需要稍微调整一下,binary_data 字符串需要包装在一个名为 'state'

的对象中
{ "state": { "binary_data": "ewogICJhUHJvcCI6ICJhVmFsdWUiCn0=" }, "gateway_info": {"delegated_device_id": "gw_device_1"}}

然后 curl 按预期工作。