"Invalid JSON payload received" Error when I send a command to the SDM API for a Google Nest Device using PHP curl

1.4k views Asked by At

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

There are 1 answers

0
Artur Fischer On

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