2017-07-27 260 views
3

,因为我没有与模拟器请求我做以上为
https://developers.google.com/actions/identity/account-linking#json
请求的登入帮手无法解析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(); 

我得到一个错误,说得到一个的accessToken

模拟器响应

" 

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字段不重复,不能启动列表\””

回答

4

对于初学者来说 - 这是一个来自Simulator的相当不错的错误消息。它会告诉您JSON中出现错误的确切路径,因此可以使用the webhook response的文档来追踪JSON可能看起来不像预期的JSON的原因。

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

你需要强制它成为一个对象。你不能使用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); 
+0

三江源指出了这一点! :) – Elo97234c

+0

当我做到了,我只是'对不起,我没有得到任何回应。' – Elo97234c

+0

在文档中,https://developers.google.com/actions/identity/account-linking#json他们有一些名为PLACEHOLDER_FOR_SIGN_IN的东西。它应该是我的身份验证端点url吗? – Elo97234c