[英]Error in Implementation of Report State for Google Home
As suggested in codelabs sample code I am implementing report state as follows: 如代码实验室示例代码中所建议,我正在按以下方式实现报告状态:
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);
});
};
But it gives me response as follows: 但这给了我如下响应:
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT"
}
}
where value for post data is : 其中发布数据的值为:
{"requestId":"hgrwbj","agentUserId":"123","payload":{"devices":{"states":{"0123456789:01":{"on":"true"}}}}}
So I tried implementing it the other way which is as follows: 因此,我尝试了另一种实现方式,如下所示:
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);
};
But this time it only gives request ID in response: 但这一次它只给出请求ID作为响应:
{"requestId":"hgrwbj"}
This time Postdata is: 这次Postdata是:
{"requestId":"hgrwbj","agentUserId":"123","payload":{"devices":{"states": {"0123456789:01":{"on":true}}}}}
Any suggestion on where I am issing to get the correct response ? 关于我在哪里得到正确答复的任何建议? Thanks in advance .
提前致谢 。
In your report state: 在您的报告状态中:
"on": "true"
You are sending the property "true" as a string. 您正在将属性“ true”作为字符串发送。 The server expects a boolean type, without quotes:
服务器需要一个布尔类型,不带引号:
"on": true
That should work as expected. 那应该按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.