2017-07-27 264 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