taishnore
taishnore

Reputation: 41

Nodemailer gmail ouath issue

A few hours ago, I was able to send gmails from my application. I have a refresh_token and an access_token, yet when I try to send an email, I get the error: err: Error: Can't create new access token for user.

It seems to be a problem with nodemailer, not with my google oauth2 flow. Again, this was working earlier, so I'm not entirely sure why it's breaking now. If I've not included useful info, let me know and I'll update.

I understand that nodemailer will try to create it's own access_token if i don't provide it with one, but here I'm providing it with both the access_token and with the refresh_token. I've tried excluding one and the other, but I am still getting the same error.

Here is my code:

  const user = req.user;

  const myEmail = user.google.email;
  const refresh_token = user.google.refresh_token;

  const employee = await Employee.findOne({ _id: req.body.employeeId });

  const employeeEmail = employee.email;
  try {
    const oauth2Client = new OAuth2(
      process.env.OAUTH_CLIENT,
      process.env.OAUTH_SECRET,
      process.env.REDIRECT_URI
    );

    oauth2Client.setCredentials({
      refresh_token: refresh_token
    });

    let testResponse = await oauth2Client.getRequestHeaders();

    let access_token = testResponse.Authorization.split(" ")[1];

    const smtpTransport = nodemailer.createTransport({
      service: "gmail",
      tls: {
        rejectUnauthorized: false
      },
      auth: {
        type: "OAuth2",
        user: myEmail,
        clientId: process.env.OAUTH_CLIENT,
        clientSecret: process.env.OAUTH_SECRET,
        refresh_token: refresh_token,
        access_token: access_token
      }
    });

    const mailOptions = {
      from: myEmail,
      to: employeeEmail,
      subject: req.body.emailObj.subject,
      text: req.body.emailObj.message
    };

    await smtpTransport.sendMail(mailOptions, async (err, result) => {
      if (err) {
        return smtpTransport.close();
      }


      employee.response = false;
      await employee.save();

      res.send({ result });
    });
  } catch (err) {
    res.status(400).send(err);
  }
});

Here is the entire error:

  err: Error: Can't create new access token for user
      at XOAuth2.generateToken (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/xoauth2/index.js:179:33)
      at XOAuth2.getToken (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/xoauth2/index.js:123:18)
      at SMTPConnection._handleXOauth2Token (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-connection/index.js:1679:27)
      at SMTPConnection.login (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-connection/index.js:534:22)
      at /Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-transport/index.js:271:32
      at SMTPConnection.<anonymous> (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-connection/index.js:209:17)
      at Object.onceWrapper (events.js:288:20)
      at SMTPConnection.emit (events.js:200:13)
      at SMTPConnection._actionEHLO (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-connection/index.js:1298:14)
      at SMTPConnection._processResponse (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-connection/index.js:929:20)
      at SMTPConnection._onData (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-connection/index.js:736:14)
      at TLSSocket.SMTPConnection._onSocketData (/Users/taimur/Coding-Projects/job-search-tracker/node_modules/nodemailer/lib/smtp-connection/index.js:189:44)
      at TLSSocket.emit (events.js:200:13)
      at addChunk (_stream_readable.js:294:12)
      at readableAddChunk (_stream_readable.js:275:11)
      at TLSSocket.Readable.push (_stream_readable.js:210:10) {
    code: 'EAUTH',
    command: 'AUTH XOAUTH2'
  }```


Upvotes: 3

Views: 682

Answers (1)

taishnore
taishnore

Reputation: 41

I figured it out, pretty quickly. In the .createTransport auth options, I was snake-casing the keys instead of camel-casing. how stupid.

Upvotes: 1

Related Questions