Как предлагается в примере кода 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);
};
Но на этот раз он только дает идентификатор запроса в ответ:
{"requestId":"hgrwbj"}
На этот раз Postdata:
{"requestId":"hgrwbj","agentUserId":"123","payload":{"devices":{"states": {"0123456789:01":{"on":true}}}}}
Любое предложение о том, где я пытаюсь получить правильный ответ? Заранее спасибо.