尝试访问Google云语音API时{J}有效负载出错

时间:2017-09-05 17:15:14

标签: google-cloud-platform google-speech-api gcp

{
  "config": {
      "encoding":"FLAC",  
      "sample_rate": 16000,  
      "language_code": "en-US"
  },
  "audio": {
      "uri":"gs://speechtotextapi/audio_file.flac"
  }
}

curl -s -X POST -H" Content-Type:application / json" --data-binary @ request.json \  " https://speech.googleapis.com/v1/speech:recognize?key= $ {API_KEY}"

最初我将mp4文件转换为FLAC格式,然后将其上传到存储桶。这是api回应。

{   "错误":{     "代码":400,     " message":"收到无效的JSON有效负载。未知名称\" sample_rate \" at' config':找不到字段。",     " status":" INVALID_ARGUMENT",     "详情":[       {         " @ type":" type.googleapis.com/google.rpc.BadRequest",         " fieldViolations":[           {             " field":" config",             "说明":"收到无效的JSON有效负载。未知名称\" sample_rate \" at' config':找不到字段。"           }         ]

1 个答案:

答案 0 :(得分:0)

您需要将格式正确的输入构造为数组,然后对其进行json编码。例如,要发送文件,base64将其编码为" content"并按照所示提交给API:

$ upload = base64_encode(file_get_contents(NAME_OF_YOUR_AUDIO_FILE));

"音频" =>阵列(         "内容" => $上传,     )