Google Translate API text-to-speech: http requests forbidden
Asked Answered
M

5

12

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?

Miss answered 2/6, 2015 at 15:5 Comment(0)
F
5

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

Factitious answered 2/6, 2015 at 15:8 Comment(10)
Thank you. I've looked at their API docs and didn't see any endpoints for getting text-to-speech files. Am I missing that? I'd be willing to pay if I can access the audio. Btw, you still can download the audio from the deprecated translate_tts URI using wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world"Miss
@Growler it's not an official api but still takes an api key. You can try get passed it using techniques from here: #12883830. You're likely to get blocked often once you make requests with over 100 characters.Factitious
@Growler I've seen that, you're likely to get blocked using it once Google detects how youre using the requests.Factitious
That SO post contains deprecated techniques that I used original when I posted this. The most updated technique there is from weston.ruter.net/2009/12/12/google-ttsMiss
@Growler that SO post is kept up to date and the link youve mentioned is from that post which still does work.Factitious
@Growler You will stil need to hide your referrer as mentioned in the SO post.Factitious
That would help bypass the 403 errors on HTTP request that I've been getting? How would I structure my query/code to hide the referrer? The goal here is to use the text-to-speech audio in my app when a user hovers over the word.Miss
@Growler You would hide the referrer using the library you use to make the HTTP request to fetch that mp3 file by making sure the referrer header does not exist. Also keep in mind they still bock requests once they detect it is being used for an API. You can't realistically create a stable app off this without an api key.Factitious
Let us continue this discussion in chat.Miss
I've gotten an API key and reposted here: #30930018. If I have an API key, do I still need to hide referrer header? If so, how?Miss
A
30

You can get anything sound, I found it: https://translate.google.com.vn/translate_tts?ie=UTF-8&q=ANYTHING_TEXT&tl=en&client=tw-ob Note: client=tw-ob

Albertson answered 14/10, 2017 at 3:33 Comment(2)
How did you find this?Sternwheeler
@Sternwheeler you can find it by using Chrome's Network web tool. It logs the request when you press the text-to-speech button. If it doesn't log, you need to press the gray button on top left corner on the web tools. It will turn red and will start recording.Alba
F
5

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

Factitious answered 2/6, 2015 at 15:8 Comment(10)
Thank you. I've looked at their API docs and didn't see any endpoints for getting text-to-speech files. Am I missing that? I'd be willing to pay if I can access the audio. Btw, you still can download the audio from the deprecated translate_tts URI using wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world"Miss
@Growler it's not an official api but still takes an api key. You can try get passed it using techniques from here: #12883830. You're likely to get blocked often once you make requests with over 100 characters.Factitious
@Growler I've seen that, you're likely to get blocked using it once Google detects how youre using the requests.Factitious
That SO post contains deprecated techniques that I used original when I posted this. The most updated technique there is from weston.ruter.net/2009/12/12/google-ttsMiss
@Growler that SO post is kept up to date and the link youve mentioned is from that post which still does work.Factitious
@Growler You will stil need to hide your referrer as mentioned in the SO post.Factitious
That would help bypass the 403 errors on HTTP request that I've been getting? How would I structure my query/code to hide the referrer? The goal here is to use the text-to-speech audio in my app when a user hovers over the word.Miss
@Growler You would hide the referrer using the library you use to make the HTTP request to fetch that mp3 file by making sure the referrer header does not exist. Also keep in mind they still bock requests once they detect it is being used for an API. You can't realistically create a stable app off this without an api key.Factitious
Let us continue this discussion in chat.Miss
I've gotten an API key and reposted here: #30930018. If I have an API key, do I still need to hide referrer header? If so, how?Miss
L
1

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();
Ledoux answered 8/9, 2022 at 3:41 Comment(0)
E
0
<!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>
Engud answered 18/5, 2022 at 3:35 Comment(3)
100% working code......Engud
translate.google.com.vn/… Note: client=tw-obEngud
My experience is It's only working local html file... but not working on localhost or cpanel...Engud
E
0
    <!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>
Engud answered 18/5, 2022 at 4:3 Comment(1)
100% working code... also test in CPanel and Local host... Mobile browser also working fine...Engud

© 2022 - 2024 — McMap. All rights reserved.