简体   繁体   中英

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

I am making a language learning web app that when you hover over the word, it pronounces it for you. I'd like to access the native speaker translations from Google Translate API.

I've found this resource which gives http://translate.google.com/translate_tts as the base URL and tl for target language and q for the query string.

This works awesome when I just access it in the browser, http://translate.google.com/translate_tts?tl=zh-CN&q =你好, but any httprequests for my app return a 403 Forbidden error.

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.

Is there a formal Google API for text-to-speech associated with their payment plans for traditional Google Translate API that I'm just not finding? Or is there a way to get and play this audio somehow?

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

That resource is dated. Google deprecated free access to the language translate api. Now an API key is needed. The api is also now in V2.

Without an api key you will receive the 403 forbidden error.

More information here: https://cloud.google.com/translate/docs

<!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>

You are only allowed to use on google translate domain by web tools:

var elText = 'こんにちは世界';
var language = 'ja-JP';
var source = 'https://translate.google.com/translate_tts?tl=' +
    language +
    '&q=' +
    encodeURIComponent(elText) +
    '&client=tw-ob';
var audio = new Audio(source);
audio.play();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM