iamtraction / google-translate

🈯 A Node.JS library to consume Google Translate API for free.
https://npmjs.com/@iamtraction/google-translate
MIT License
540 stars 73 forks source link

SyntaxError: Unexpected token < in JSON at position 0 #47

Open pixysadfee opened 1 year ago

pixysadfee commented 1 year ago

I used a simple code to translate strings using @iamtraction/google-translate

    const translate = require('@iamtraction/google-translate');

    module.exports = function({ string, from, to }) {

        return new Promise(resolve => { 

            run();

            async function run() {
                try {
                    const res = await translate(string, { from, to });
                    return resolve(res.text);
                } catch(err) {
                    console.log(err);
                    run();
                }
            }

        });

    }

and I used the above function as:

    const bigTranslation = await googleTranslator({ string: 'It looks like we’ll have to stand', from: 'en', to: 'es' });

    return console.log(bigTranslation);

It worked perfectly till hours ago but now I get this error:

SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at consumeEnd (C:\inetpub\wwwroot\Server\node_modules\undici\lib\api\readable.js:237:20) at BodyReadable. (C:\inetpub\wwwroot\Server\node_modules\undici\lib\api\readable.js:219:7) at BodyReadable.emit (node:events:513:28) at BodyReadable.emit (C:\inetpub\wwwroot\Server\node_modules\undici\lib\api\readable.js:66:18) at endReadableNT (node:internal/streams/readable:1359:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21)

Why This is happening ?

Atamg1994 commented 1 year ago

i have same problem see screnshot Screenshot_10 the problem is that there were a lot of translation requests and for this reason you need to pass a test for a robot, you need to take this into account and connect anti-captcha services .. if possible with a paid API

after manually checking, I received a file with the answer, so the problem is in the captcha

while for myself I made an output to the console if(response.statusCode == 302){ console.log(["capcha",response.headers?.location]) } Screenshot_11