[英]Error in Implementation of Report State for Google Home
如代碼實驗室示例代碼中所建議,我正在按以下方式實現報告狀態:
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}}}}}
關於我在哪里得到正確答復的任何建議? 提前致謝 。
在您的報告狀態中:
"on": "true"
您正在將屬性“ true”作為字符串發送。 服務器需要一個布爾類型,不帶引號:
"on": true
那應該按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.