无法解析 Json 响应

Unparseable Json Response

由于我没有通过模拟器请求获得 accessToken,所以我按照
中所述执行了上述操作 https://developers.google.com/actions/identity/account-linking#json
请求登录助手

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

我收到一个错误

模拟器响应

"

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

错误

API 版本 2:无法解析 JSON 响应字符串 'INVALID_ARGUMENT' 错误:\"expected_inputs[0].possible_intents[0]: Proto field is not repeating, cannot start list.\"."

对于初学者 - 这是来自模拟器的非常好的错误消息。它会告诉您 JSON 中出现错误的确切路径,因此可以使用 the webhook response 的文档来追踪为什么您的 JSON 可能看起来不像预期的 [=32] =].

在这种情况下,inputValueData 的值必须是 JSON 对象,而不是 JSON 数组。默认情况下 PHP 的 json_encode() 函数假定空 PHP 数组是空 JSON 数组(这是 noInputPrompts [=38= 的正确假设]).

你需要强制它成为一个对象。你不能使用JSON_FORCE_OBJECT设置,因为这样noInputPrompts会变成一个对象,这也是错误的。

您需要使用

等语法将数组转换为对象
(object)array()

所以你的代码看起来像

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