0

正如codelabs示例代码中所建议的,我正在实现报告状态,如下所示:

function reportState(devid, actionVal) {
  console.log('INSIDE REPORT STATE');
  if (!app.jwt) {
    console.warn('Service account key is not configured');
    console.warn('Report state is unavailable');
    return;
  }




const postData = {
    requestId: 'hgrwbj', /* Any unique ID */
    agentUserId: '123', /* Hardcoded user ID */
    payload: {
      devices: {
        states: {
          [devid]: {
            on: actionVal,
          },
        },
      },
    },
  };



  console.log('POSTDATA %j', postData);

  return app.reportState(postData)
     .then((data) => {
       console.log('Report state came back');
       console.info(data);
     });
};

但它给了我如下回应:

{
  "error": {
    "code": 400,
    "message": "Request contains an invalid argument.",
    "status": "INVALID_ARGUMENT"
  }
}

发布数据的值为:

{"requestId":"hgrwbj","agentUserId":"123","payload":{"devices":{"states":{"0123456789:01":{"on":"true"}}}}}

所以我尝试以另一种方式实现它,如下所示:

function reportState(devid, actionVal) {
  console.log('INSIDE REPORT STATE');
  if (!app.jwt) {
    console.warn('Service account key is not configured');
    console.warn('Report state is unavailable');
    return;
  }

  const jwtClient = new google.auth.JWT(
    jwt.client_email,
    null,
    jwt.private_key,
    ['https://www.googleapis.com/auth/homegraph'],
    null
  );
  console.log('JWT',jwt);
  console.log('JWTCLIENT',jwtClient);

  const postData = {
    requestId: 'hgrwbj', /* Any unique ID */
    agentUserId: '123', /* Hardcoded user ID */
    payload: {
      devices: {
        states: {
          [devid]: {
            on: actionVal,
          },
        },
      },
    },
  };

  jwtClient.authorize((err, tokens) => {
    if (err) {
      console.error(err);
      return;
    }
    console.log('ACCESS TOKEN',tokens.access_token);
    const options = {
      hostname: 'homegraph.googleapis.com',
      port: 443,
      path: '/v1/devices:reportStateAndNotification',
      method: 'POST',
      headers: {
        Authorization: ` Bearer ${tokens.access_token}`,
      },
    };
    return new Promise((resolve, reject) => {
      let responseData = '';
      const req = https.request(options, (res) => {
        res.on('data', (d) => {
          responseData += d.toString();
        });
        res.on('end', () => {
          resolve(responseData);
        });
      });
      req.on('error', (e) => {
        reject(e);
      });
      // Write data to request body
      req.write(JSON.stringify(postData));
      req.end();
    }).then((data) => {
      console.info('REPORT STATE RESPONsE', data);
    });
  });


  console.log('POSTDATA %j', postData);

};

但这次它只给出请求 ID 作为响应:

{"requestId":"hgrwbj"} 

这次 Postdata 是:

{"requestId":"hgrwbj","agentUserId":"123","payload":{"devices":{"states": {"0123456789:01":{"on":true}}}}}

关于我在哪里获得正确回复的任何建议?提前致谢 。

4

1 回答 1

0

在您的报告状态中:

"on": "true"

您将属性“true”作为字符串发送。服务器需要一个布尔类型,不带引号:

"on": true

这应该按预期工作。

于 2018-11-13T18:56:06.297 回答