node-imap icon indicating copy to clipboard operation
node-imap copied to clipboard

Invalid credentials (Failure) - Gmail

Open ghost opened this issue 1 year ago • 12 comments

Good morning,

could you help me, I am not able to connect to the gmail server, below the code.

describe('Check connection to email server', function () {

it('It should return a successful connection message', async function () {

    var config = {
        user: '[email protected]',
        password: 'xxxxxx',
        host: 'imap.gmail.com',
        port: 993,
        tls: true,
    };
    
    async function checkEmailConnection() {
        const imap = new Imap(config);
    
        return new Promise((resolve, reject) => {
            imap.once('ready', () => {
                imap.end();
                resolve('Conexão bem-sucedida com o servidor de e-mail!');
            });
    
            imap.once('error', (error) => {
                reject(`Erro na conexão com o servidor de e-mail: ${error}`);
            });
    
            imap.connect();
        });
    }

    const message = await checkEmailConnection();
})

})

after executing the error is returned thrown: "Erro na conexão com o servidor de e-mail: Error: Invalid credentials (Failure)"

thank you very much

ghost avatar Jun 02 '23 15:06 ghost

Perhaps you need to use the xoauth2 config setting for OAuth 2-based authentication instead of using a password.

mscdex avatar Jun 02 '23 15:06 mscdex

@mscdex I did the configuration, but now it is showing another error:

this is the code const credentials = { client_id: 'xxx', client_secret: 'xxx', redirect_uri: 'http://localhost', };

    const oauth2Client = new google.auth.OAuth2(
        credentials.client_id,
        credentials.client_secret,
        credentials.redirect_uri
    );

    const auth = {
        user: '[email protected]',
        xoauth2: oauth2Client,
    };

    const imap = new Imap({
        user: auth.user,
        xoauth2: oauth2Client,
        host: 'imap.gmail.com',
        port: 993,
        tls: true,
        authTimeout: 30000,
        tlsOptions: {servername: 'imap.gmail.com', rejectUnauthorized: false,}
    });

    function openInbox(cb) {
        imap.openBox('INBOX', true, cb);
    }

    async function checkEmailConnection() {
        imap.connect();
    }

    await checkEmailConnection()

this is the error

return str.replace(RE_BACKSLASH, '\\').replace(RE_DBLQUOTE, '\"'); ^

TypeError: str.replace is not a function at escape (C:\aqa-argopay-api\node_modules\imap\lib\Connection.js:1804:14) at Connection. (C:\aqa-argopay-api\node_modules\imap\lib\Connection.js:1672:24) at Connection.Object..Connection._resTagged (C:\aqa-argopay-api\node_modules\imap\lib\Connection.js:1535:22) at Parser. (C:\aqa-argopay-api\node_modules\imap\lib\Connection.js:194:10) at Parser.emit (node:events:527:28) at Parser.Object..Parser._resTagged (C:\aqa-argopay-api\node_modules\imap\lib\Parser.js:175:10) at Parser.Object..Parser._parse (C:\aqa-argopay-api\node_modules\imap\lib\Parser.js:139:16) at Parser.Object..Parser._tryread (C:\aqa-argopay-api\node_modules\imap\lib\Parser.js:82:15) at TLSSocket.Parser.cbReadable (C:\aqa-argopay-api\node_modules\imap\lib\Parser.js:53:12) at TLSSocket.emit (node:events:527:28) at emitReadable (node:internal/streams/readable:578:12) at processTicksAndRejections (node:internal/process/task_queues:82:21)

ghost avatar Jun 02 '23 21:06 ghost

same issue

zhyd1997 avatar Jun 28 '23 10:06 zhyd1997

Try to use app password, works for me: https://support.google.com/accounts/answer/185833

Screenshot 2023-07-01 at 12 13 41

zhyd1997 avatar Jul 01 '23 04:07 zhyd1997

Hey all, I seem to have the same problem. Has anyone managed to find a workaround?

Vastradamus avatar Jul 28 '23 06:07 Vastradamus

You need 2FA and App password :)

https://github.com/mscdex/node-imap/issues/879

LukasLewandowski avatar Jul 28 '23 08:07 LukasLewandowski

That's what I tried first, but I was getting this: Error: No supported authentication method(s) available. Unable to login.

So I assumed I need to use xoauth2. Is there something I'm missing?

Vastradamus avatar Jul 28 '23 11:07 Vastradamus

@Vastradamus It worked for me, with 2FA and password

var imap = new Imap({ user: '', password: '', host: 'imap.gmail.com', port: 993, tls: true, authTimeout: 100000, tlsOptions: { rejectUnauthorized: false, servername: 'imap.gmail.com' }, });

LukasLewandowski avatar Aug 03 '23 08:08 LukasLewandowski

@Vastradamus I was dealing with Invalid credential error but I was able to make it work using App password.An app password is a 16-digit passcode that gives a less secure app or device permission to access your Google Account. Here is the link how to set up the password https://support.google.com/mail/answer/185833?hl=en Finally, replace the generated password in the email config password.

QA1980 avatar Oct 31 '23 20:10 QA1980

I have resolved this error in 2 step:

  • IMAP Enabled from "Email Setting"
  • Less secure app access Enabled from "Google Account"

yracnet avatar Feb 06 '24 15:02 yracnet

I have resolved this error in 2 step:

  • IMAP Enabled from "Email Setting"
  • Less secure app access Enabled from "Google Account"

This feature appears to have been discontinued by Google.

YangYgYang avatar Apr 23 '24 03:04 YangYgYang

This has been verified by me that the feature have been or maybe discontinued by Google See this Please close this issue.

However, I believe this can be fixed by creating a service account and connecting to it.

Saksham-Chauhan avatar Aug 12 '24 12:08 Saksham-Chauhan