GCP IoTCore无法使用网关和HTTP桥解析有效负载

时间:2019-07-18 19:06:10

标签: google-cloud-platform google-cloud-iot

到目前为止已采取的步骤

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

到目前为止工作正常。

现在,我想使用my_first_gateway的私钥通过我的网关使用HTTP桥通过网关将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”替换为“ ewogICJhUHvcvc6ICJhVmFsdWUiCn0”时,我执行以下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."
          }
        ]
      }
    ]
  }
}

有趣的是:还有另一个“端点”用于将事件发布到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'

我想念什么吗? 任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

实际上,谷歌提供的卷曲是不正确的。 有效负载需要稍作调整,binary_data字符串需要包装在一个称为“状态”的对象中

{ "state": { "binary_data": "ewogICJhUHJvcCI6ICJhVmFsdWUiCn0=" }, "gateway_info": {"delegated_device_id": "gw_device_1"}}

然后卷曲将按预期工作。