Google Translate API - No Access Control Origin with Text to Speech
Asked Answered
R

0

5

I posted this question on SO to get Google Translate Text-To-Speech to work.

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

I was told I needed a key and to enable billing. I've since done that. I know billing is enabled because, using their specified endpoint for words-only translations (not narrated speech) (GET https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world), I'm able to get a response both in DHC and in my application $.get:

enter image description here

In my original question (above), I was told If I get an API key, I would no longer be blocked from getting text-to-speech. I tested the request for Text-to-speech in DHC and Postman:

https://translate.google.com/translate_tts?key=myKeyHere&ie=utf-8&tl=zh-CN&q=你好

And got a 200:

enter image description here

Excellent. However, in my application, I make a get request:

    $.get('https://translate.google.com/translate_tts?key='+myKey+'&ie=utf-8&tl=en&q=Hello+world',
        function (returned_data) {

I get blocked:

No 'Access-Control-Allow-Origin' header

Why is this?

Reinke answered 19/6, 2015 at 3:36 Comment(2)
Well, Its a security from within your browser. If the response of your request doesn't contain the domain from which your requesting Or ' * ' in the Access-Control-Allow-Origin header, The browser prevents your scripts from reading it.Laurettelauri
And when you have key probably Access-Control-Allow-Origin header will allow you from only the specific domain..Laurettelauri

© 2022 - 2024 — McMap. All rights reserved.