【发布时间】:2020-10-22 12:40:05
【问题描述】:
我喜欢使用 SDM API 的命令功能。我必须用 PHP 做 Google 文档需要以下语法:
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer access-token' \
--data-raw '{
"command" : "sdm.devices.commands.CameraLiveStream.GenerateRtspStream",
"params" : {}
}'
我的 PHP 代码如下所示:
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
此命令产生以下错误(我将事件 ID 替换为 XYZ):
Array
(
[error] => Array
(
[code] => 400
[message] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ...."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
[status] => INVALID_ARGUMENT
[details] => Array
(
[0] => Array
(
[@type] => type.googleapis.com/google.rpc.BadRequest
[fieldViolations] => Array
(
[0] => Array
(
[description] => Invalid JSON payload received. Unknown name "{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"XYZ..."}}": Cannot bind query parameter. Field '{"command":"sdm' could not be found in request message.
)
)
)
)
)
)
我做了以下测试:
Google 提供了一个终端功能,它允许我输入文档中描述的原始语法(见上文),而无需依赖 PHP Curl。
在手动测试成功后,我使用我的 PHP 代码创建了原始数据:
$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
'params' => $sub
);
$PostData = json_encode ($PostData_array);
和网址:
$url = 'https://smartdevicemanagement.googleapis.com/v1/enterprises/'.$project_id.'/devices/'.$device_id.':executeCommand';
我手动将字符串复制到这个结构中:
curl -X POST \
'' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ' \
--data-raw ''
然后我将以下代码复制粘贴到终端中,它可以工作(敏感信息已删除):
curl -X POST \
'https://smartdevicemanagement.googleapis.com/v1/enterprises/f779e361..../devices/AVP.......:executeCommand' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer ya29........' \
--data-raw '{"command":"sdm.devices.commands.CameraLiveStream.GenerateRtspStream","params":{"eventId":"Ci........."}}'
所以错误一定来自我的卷曲代码:
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Authorization: Bearer '.$access_token ));
curl_setopt($curl_handle, CURLOPT_POST, true);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $PostData);
curl_setopt($curl_handle, CURLOPT_URL,$url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
$fileContent = curl_exec($curl_handle);
curl_close($curl_handle);
【问题讨论】:
-
错误信息非常明确。
-
这是真的,但它没有帮助:我的语法完全符合要求。在过去的 12 个小时里,我一直在研究这个问题并尝试了各种方法,不幸的是没有成功
-
你的 $url var 的值是多少?另外,请帮助我们解决此类问题。放一个指向 API 参考资料的链接,这样我们就不必搜索了。
-
你的权利,我很抱歉。主要 API 描述可以在这里找到:developers.google.com/nest/device-access/use-the-api 详细描述在这里:developers.google.com/nest/device-access/traits/device/… 我使用了以下 URL:$url = 'smartdevicemanagement.googleapis.com/v1/enterprises/…';
-
Arthur,您能否通过编辑令牌和各种 ID 值发布完整的出站请求?如果没有看到完整的出站请求,就很难诊断。
标签: php php-curl nest-device-access google-sdm-api