Hello, I tried to fetch google translate api with wix-code, but I always get a 400 even if it works in POSTMAN…
Please Help
This is my code…
In a backend file named fetch.jsw I have those methods: ( The getBitlyUrl method works, the translate method DO NOT work) I have no restriction on my key in google api.
export function fetchUrl(url, method, body) {
return fetch(encodeURI(url), {headers: {}, method: method, body: body})
.then(response => response.json())
.then(json => json);
}
export function translate(text) {
const url = “https://translation.googleapis.com/language/translate/v2”;
const key = ‘<I entered my key here!>’;
let fullUrl = url + ‘?key=’ + key;
let body = {
q : text,
target: “fr”
};
return fetchUrl(fullUrl, ‘post’, body);
}
export function getBitlyUrl(title) {
const url = “https://api-ssl.bitly.com/v3/shorten”;
const key = “”;
let longUrl = “https://www.hello.com/” + encodeURIComponent(title);
let fullUrl = url + ‘?longUrl=’ + longUrl + ‘&access_token=’ + key;
return fetchUrl(fullUrl, ‘get’);
}
Then on my page I have a button to translate a text
export function buttonTranslate_click(event) {
translate([$w(‘#inputEnglish’).value]).then( (result) => {
$w(‘#inputFrench’).value = result.data.translations[0].translatedText;
}). catch ( (err) => {
console.log(err);
});
}
When I inspect the result of the fetch, the WixFetchResponse.status is 400.
Thanks for your help!