0

I need to get (any) OK-response from Yandex SpeechKit.API. I got API-KEY (valid) and successfully used it and received responses via NodeJS and even GitBash (see links to screens below). But browsers show me 401: enter image description here There're rules for API requests: docs. My JS code for browsers:

let api_key = '*********c_Ujs9scAhJVVZOs2xjnbvevqj6OFFm';
let params = new URLSearchParams();

const text = 'Привет!';
params.append('text', text);
params.append('voice', 'jane');
params.append('emotion', 'good');
params.append('lang', 'ru-RU');
params.append('speed', '1.0');
params.append('format', 'oggopus');

const fetchButton = document.getElementById("fetchButton");

fetchButton.onclick = () => {

  fetch('https://tts.api.cloud.yandex.net/speech/v1/tts:synthesize', {
    method: 'POST',
    body: params,
    mode: "no-cors",
    headers: {
      //'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Api-Key ' + api_key,
    },
  }).then((res) => {
    console.log(res);
  }).catch((err) => {
    console.error(`!!! + ${err} + !!!!!!!!`)
  });

}

There're code examples for Node and Bash:

[nodeJS code][3] gitBash code

Vladislav Varslavans
  • 2,775
  • 4
  • 18
  • 33
svegger
  • 21
  • 3
  • Hmm, the [documentation](https://cloud.yandex.com/docs/speechkit/concepts/auth) seems to suggest that the Authorization header should start `Bearer` – phuzi Jan 25 '21 at 21:34

1 Answers1

0

You said:

mode: "no-cors",            //required

… this means "I am making a cross-origin request, but I am not doing anything that requires the server grant me permission with CORS so instead of throwing errors, fail silently".

You also said:

'Authorization': 'Api-Key ' + api_key,

Setting this header requires permission from CORS. Since you are in no-cors mode it automatically and silently fails to be set.

Since you didn't set the authorisation key, you get a 401 Unauthorised response from the server.


You can't use no-cors mode here, even if you have a comment saying it is required.

There is some further reading on CORS you may find useful. Note that since the service you are using requires an API key and imposes limits, it likely isn't intended for use from a browser and you should implement a server-side solution instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • thanks a lot. How do you think, if i upload my NodeJs file (which is successfully working with) to Heroku (for example) and will request to it from browser, will it work? – svegger Jan 27 '21 at 10:45