如代码实验室示例代码中所建议,我正在按以下方式实现报告状态:
<?php
// Get a connection for the database
require_once('mysqli_connect.php');
// Create a query for the database
$query = "SELECT username, message FROM messages";
// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);
// If the query executed properly proceed
if($response){
echo '<table align="left"
cellspacing="5" cellpadding="8">
<tr><td align="left"><b>username</b></td>
<td align="left"><b>Message</b></td></tr>';
// mysqli_fetch_array will return a row of data from the query
// until no further data is available
while($row = mysqli_fetch_array($response)){
echo '<tr><td align="left">' .
$row['username'] . '</td><td align="left">' .
$row['message'] . '</td><td align="left">';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query<br />";
echo mysqli_error($dbc);
}
// Close connection to the database
mysqli_close($dbc);
?>
但这给了我以下答复:
<?php
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', 'password');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'Cheese');
$dbc = @mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
OR die('Could not connect to MySQL: ' .
mysqli_connect_error());
?>
其中发布数据的值为:
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"
}
}
但是这次它只给出请求ID作为响应:
{"requestId":"hgrwbj","agentUserId":"123","payload":{"devices":{"states":{"0123456789:01":{"on":"true"}}}}}
这次Postdata是:
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);
};
关于我在哪里得到正确答案的任何建议?预先感谢。
答案 0 :(得分:0)
在您的报告状态:
"on": "true"
您正在将属性“ true”作为字符串发送。服务器需要一个布尔类型,不带引号:
"on": true
应该可以正常工作。