几个小时前,我能够从我的应用程序发送gmail。我有一个refresh_token
和access_token
,但是当我尝试发送电子邮件时,出现错误:err: Error: Can't create new access token for user
。
这似乎是nodemailer的问题,而不是我的Google oauth2流。再说一次,这是较早的工作,所以我不完全确定为什么现在坏了。如果我没有提供有用的信息,请告诉我,我会进行更新。
我了解到,如果我不提供它,nodemailer会尝试创建它自己的access_token,但是在这里,我同时为它提供了access_token
和refresh_token
。我尝试排除一个和另一个,但是仍然出现相同的错误。
这是我的代码:
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);
}
});
这是整个错误:
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'
}```
最佳答案
我很快就知道了。在.createTransport
auth选项中,我将密钥装在外壳中而不是骆驼式安装。真蠢
关于node.js - Nodemailer gmail oauth问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58986893/