I have two query code examples that return the same error related to the API key, but it is correct.
Example of error:
code: 16,
message: 'The token is invalid',
Js
const IAM_TOKEN = 'IAM_TOKEN';
const folder_id = 'folder_id';
const target_language = 'ru';
const texts = ["Hello", "World"];
const body = {
targetLanguageCode: target_language,
texts: texts,
folderId: folder_id,
};
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${IAM_TOKEN}`,
};
fetch('https://translate.api.cloud.yandex.net/translate/v2/translate', {
method: 'POST',
headers: headers,
body: JSON.stringify(body),
})
.then(response => response.json())
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Ошибка при выполнении запроса:', error);
});
Python
import requests
IAM_TOKEN = 'IAM_TOKEN'
folder_id = 'folder_id'
target_language = 'ru'
texts = ["Hello", "World"]
body = {
"targetLanguageCode": target_language,
"texts": texts,
"folderId": folder_id,
}
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {0}".format(IAM_TOKEN)
}
response = requests.post('https://translate.api.cloud.yandex.net/translate/v2/translate',
json = body,
headers = headers
)
print(response.text)
Tried different XMLHttpRequest, fetch, axios queries