AidanWelch / google-translate-api

An updated free and unlimited API for Google Translate :dollar: :no_entry_sign:
MIT License
143 stars 21 forks source link

CORS policy #11

Closed lucaspessivaldo closed 2 years ago

lucaspessivaldo commented 2 years ago

I'm using ReactJs. When I use the request example. I'm getting CORS error.

import translate from 'google-translate-api-x';

const res = await translate('Ik spreek Engels', { to: 'en' });

console.log(res.text); //=> I speak English
console.log(res.from.language.iso);  //=> nl

That code return

Access to fetch at 'https://translate.google.com/' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Is that an API error or I need to use some param on the request translate()?

AidanWelch commented 2 years ago

No, this is not an error with this package, this is an intended feature of browsers called the Same-Origin Policy. It is critical for protecting sensitive user information. From a normal webpage context it is not possible to use this package, it can be used from a a browser extension background script or an Electron project though.

AidanWelch commented 2 years ago

For an example of usage in a browser extension see here

lucaspessivaldo commented 2 years ago

Thankst to answer, I'll check it.