0

I like to use the command function of the SDM API. I have to do it in PHP The Google Documentation requires the following Syntax :

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" : {}
  }'

My PHP Code looks like this :

$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);

This command generates the following Error (I replaced the Event ID with 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.
                                        )

                                )

                        )

                )

        )

)

I have done the following Test :

Google provides a Terminal function which allows me to enter the original syntax described in the Documentation (see above) without relying on PHP Curl.

After a successful manual Test I used my PHP code to create the RAW Data :

$sub ['eventId'] = $event_id;
$PostData_array = array('command' => "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" ,
                'params' => $sub               
                );

$PostData = json_encode ($PostData_array);

and the URL :

 $url = 'https://smartdevicemanagement.googleapis.com/v1/enterprises/'.$project_id.'/devices/'.$device_id.':executeCommand'; 

I manually copied the strings into this structure :

curl -X POST \
  '' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer ' \
  --data-raw ''

       

I then copy paste the following code into the Terminal and it worked (sensitive Info deleted) :

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........."}}'

So the error must originate from my Curl Code :

$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);
  • the error message is pretty explicit. – YvesLeBorg Oct 22 '20 at 12:51
  • That's true but it doesn't help : My Syntax is exactly as requested. I have been looking at this problem for the last 12 hours and tried all sorts of things, unfortunately without success – Artur Fischer Oct 22 '20 at 14:44
  • what is the value of your $url var ? also, help us with questions like these. Put a link to the API reference material so we dont have to search. – YvesLeBorg Oct 22 '20 at 15:53
  • your right, my apologies. The principal API description can be found here : https://developers.google.com/nest/device-access/use-the-api The detailed description is here : https://developers.google.com/nest/device-access/traits/device/camera-live-stream I used the following URL : $url = 'https://smartdevicemanagement.googleapis.com/v1/enterprises/'.$project_id.'/devices/'.$device_id.':executeCommand'; – Artur Fischer Oct 22 '20 at 21:33
  • Arthur, can you post the full outbound request by editing the token and various ID values? Its hard to diagnose without seeing the full outbound request. – Anjaneesh Rayapati Oct 23 '20 at 19:33

1 Answers1

1

OK Problem is fixed. My Headers where wrong. The correct Curl Code is this :

$curl_handle=curl_init();

$headers = array(
    "POST  HTTP/1.0",
    "Content-type: application/json",
    "Accept: application/json",
    "Content-length: ".strlen($PostData),
    "Authorization:  Bearer ".$access_token
);
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $headers);
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);

Tks everybody for your response, it helped me to move in the correct direction