Request to Google Text-To-Speech API [closed]

2020-02-06 07:19发布

I have this URL: http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=Hello+world When I put it to the address bar in a browser and hit enter, I get mp3 file with synthesized speech saying "Hello world". Everything is correct.

But now I have a link in HTML that redirects to this URL, like this:

<a href="http://translate.google.com/translate_tts?ie=utf-8&tl=en&q=Hello+world">Link</a>

When I click this link, I don't get a file but an error (simple QuickTime logo). Could anyone explain me why is it so and how can I solve my problem?

1条回答
时光不老,我们不散
2楼-- · 2020-02-06 07:56

The explanation is that Google restricts the usage of this service: according to this StackOverflow answer, Google returns a 404 error if the HTTP request contains a Referer header other than an empty string.

When you type the URL in the address bar, it's ok because no Referer URL is transmitted to Google. When you click on a hyperlink, you have a referer and Google blocks your request.

NB: I tested your URL (see here), and it worked. I suspect that it was only because I have tried before by typing the URL in my address bar, and the resource was still in my browser cache or in my audio plugin (VLC) cache.

NB2: See here for a workaround.

查看更多
登录 后发表回答