Google 翻译 API 文字转语音:禁止 http 请求

Google Translate API text-to-speech: http requests forbidden

我正在制作一个语言学习网络应用程序,当您将鼠标悬停在单词上时,它会为您发音。我想访问 Google 翻译 API.

的母语翻译

我发现 this resource 给出 http://translate.google.com/translate_tts 作为目标语言的基础 URL 和 tl 以及查询字符串的 q

当我只在浏览器中访问它时,它非常有用, http://translate.google.com/translate_tts?tl=zh-CN&q=你好,但是我的应用程序的任何 httprequests return 403 禁止错误。

localhost:~ me$ wget "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world" --2015-06-02 11:02:06-- http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world Resolving translate.google.com... 173.194.123.38, 173.194.123.36, 173.194.123.32, ... Connecting to translate.google.com|173.194.123.38|:80... connected. HTTP request sent, awaiting response... 403 Forbidden 2015-06-02 11:02:07 ERROR 403: Forbidden.

是否有正式的 Google API 文本转语音与他们的传统 Google 翻译 API 支付计划相关联,我只是没有找到?或者有没有办法 get 并以某种方式播放此音频?

该资源已过时。 Google 已弃用免费访问语言翻译 api。现在需要 API 键。 api 现在也在 V2 中。

如果没有 api 密钥,您将收到 403 禁止错误。

这里有更多信息:https://cloud.google.com/translate/docs

你可以得到任何声音,我找到了: https://translate.google.com.vn/translate_tts?ie=UTF-8&q=ANYTHING_TEXT&tl=en&client=tw-ob 注:client=tw-ob

<!DOCTYPE html>
<html>
<head>
<title>Text TO Speach</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("#says").click(function(){
    var url="https://translate.google.com/translate_tts?ie=UTF-8&q="+encodeURIComponent($("#text").val())+"&tl=ta&client=tw-ob";
    $(".speech").attr('src',url).get(0).play();
    console.log("URL : "+$(".speech").attr('src'));
  });
  
});
</script>
</head>
<body>
<input type="text" name="text" id="text" value="Token Number : 2">
<button type="button" id="says">Speak</button>
<audio src="" class="speech" hidden></audio>
</body>
</html>
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    </head>
    <body>
    
    <input type="text" name="text" id="text" value="Token Number : 2">
    <button type="button" id="says">Speek</button>
    <audio src="" class="speech" hidden></audio>
    <script>
      $("#says").click(function(){
         if ('speechSynthesis' in window) {
// Speech Synthesis supported 
          var msg = new SpeechSynthesisUtterance();
    msg.text = $("#text").val();
    window.speechSynthesis.speak(msg);
    
    }else{
      // Speech Synthesis Not Supported 
      alert("Sorry, your browser doesn't support text to speech!");
    }
    });
    </script>
    </body>
    </html>