对于Google Cloud语音API中的\" ZKgdjfk .........,Base64解码失败

时间:2017-07-10 08:01:26

标签: api google-speech-api

我的request.sh代码:

#!/bin/bash

# Copyright 2017 Google Inc.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#     http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Create a request file with our JSON request in the current directory
FILENAME="request-"`date +"%s".json`
cat <<EOF > $FILENAME
{
  "config": {
    "encoding":"FLAC",
    "sampleRateHertz":16000,
    "profanityFilter": true,
    "languageCode": "en-US",
    "speechContexts": {
      "phrases": ['']
    },
    "maxAlternatives": 1
  },
  "audio": {
    "content":
    }
}
EOF

# Update the languageCode parameter if one was supplied
if [ $# -eq 1 ]
  then
    sed -i 'audio.base64' -e "s/en-US/$1/g" $FILENAME
fi

# Record an audio file, base64 encode it, and update our request object
read -p "Press enter when you're ready to record" rec
if [ -z $rec ]; then
  rec --channels=1 --bits=16 --rate=16000 audio.flac trim 0 3
  echo \"`base64 audio.flac`\" > audio.base64
  sed -i 'audio.base64' -e '/ "content":/r audio.base64' $FILENAME
fi
echo Request "file" $FILENAME created:
head -7 $FILENAME # Don't print the entire file because there's a giant base64 string
echo $'\t"Your base64 string..."\n\x00\x00}\n}'

# Call the speech API (requires an API key)
read -p $'\nPress enter when you\'re ready to call the Speech API' var
if [ -z $var ];
  then
    echo "Running the following curl command:"
    echo "curl -s -X POST -H 'Content-Type: application/json' --data-binary @${FILENAME} https://speech.googleapis.com/v1/speech:recognize?key=API_KEY"
    curl -s -X POST -H "Content-Type: application/json" --data-binary @${FILENAME} https://speech.googleapis.com/v1/speech:recognize?key=MY_API_KEY
fi

我收到错误

   {
      "error": {
        "code": 400,
        "message": "Invalid value at 'audio.content' (TYPE_BYTES), Base64 decoding failed for \"ZkxhQwAAACAAA....\"",  

"status": "INVALID_ARGUMENT",
        "details": [
          {
            "@type": "type.googleapis.com/google.rpc.BadRequest",
            "fieldViolations": [
              {
                "field": "audio.content",
                "description": "Invalid value at 'audio.content' (TYPE_BYTES), Base64 decoding failed for \"Zkxh...

当我执行request.sh bash文件时,上述错误会上升。

如何解决此错误?或者其他人有像我这样的问题吗?

1 个答案:

答案 0 :(得分:0)

看起来你也在Hakernoon尝试过Sara Robinson的代码。

我遇到了同样的问题,this post from ZodiacLeo123帮助了我。

您必须添加“-w”标志

base64 -w 0 audio.flac

这会删除额外的新行。