Elo97234c
Elo97234c

Reputation: 193

Unparseable Json Response

Since i am not getting an accessToken with the simulator Request I did the above as said in
https://developers.google.com/actions/identity/account-linking#json
Request the signin helper

    header('Content-Type: application/json');
   $askToken = array (
  'conversationToken' => '{"state":null,"data":{}}',
  'expectUserResponse' => true,
  'expectedInputs' => 
  array (
    0 => 
    array (
      'inputPrompt' => 
      array (
        'initialPrompts' => 
        array (
          0 => 
          array (
            'textToSpeech' => 'MY AUTHENTICATION END POINT URL',
          ),
        ),
        'noInputPrompts' => 
        array (
        ),
      ),
      'possibleIntents' => 
      array (
        0 => 
        array (
          'intent' => 'actions.intent.SIGN_IN',
          'inputValueData' => 
          array (
          ),
        ),
      ),
    ),
  ),
);
echo json_encode($askToken);


    exit();

I get an error

Simulator Response

"

sharedDebugInfo": [
            {
                "name": "ResponseValidation",
                "subDebugEntry": [
                    {
                        "name": "UnparseableJsonResponse",
                        "debugInfo": "API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: \"expected_inputs[0].possible_intents[0]: Proto field is not repeating, cannot start list.\"."
                    }
                ]
            }
        ]
    },
    "visualResponse": {}
}

Error

API Version 2: Failed to parse JSON response string with 'INVALID_ARGUMENT' error: \"expected_inputs[0].possible_intents[0]: Proto field is not repeating, cannot start list.\"."

Upvotes: 3

Views: 2298

Answers (1)

Prisoner
Prisoner

Reputation: 50701

For starters - that is a pretty good error message from the Simulator. It tells you the exact path in the JSON where there is an error, so one can use the documentation for the webhook response to track down why your JSON might not look like the expected JSON.

In this case, the value for inputValueData must be a JSON object, not a JSON array. By default PHP's json_encode() function assumes that empty PHP arrays are empty JSON arrays (and this is a correct assumption for the noInputPrompts property).

You need to force it to be an object. You can't use the JSON_FORCE_OBJECT setting, because then noInputPrompts would change to an object, which is also wrong.

You need to cast the array to an object using syntax such as

(object)array()

So your code would look something like

$askToken = array (
  'conversationToken' => '{"state":null,"data":{}}',
  'expectUserResponse' => true,
  'expectedInputs' => 
  array (
    0 => 
    array (
      'inputPrompt' => 
      array (
        'initialPrompts' => 
        array (
          0 => 
          array (
            'textToSpeech' => 'MY AUTHENTICATION END POINT URL',
          ),
        ),
        'noInputPrompts' => 
        array (
        ),
      ),
      'possibleIntents' => 
      array (
        0 => 
        array (
          'intent' => 'actions.intent.SIGN_IN',
          'inputValueData' => 
          (object)array (
          ),
        ),
      ),
    ),
  ),
);
echo json_encode($askToken);

Upvotes: 4

Related Questions