gpt4 book ai didi

node.js - Nodemailer gmail oauth 问题

转载 作者:太空宇宙 更新时间:2023-11-04 01:21:47 26 4
gpt4 key购买 nike

几个小时前,我能够从我的应用程序发送 gmail。我有一个 refresh_token 和一个 access_token,但当我尝试发送电子邮件时,收到错误:err: Error: Can't create new access token for user

这似乎是nodemailer的问题,而不是我的google oauth2流程的问题。再说一次,这之前是有效的,所以我不完全确定为什么它现在坏了。如果我没有包含有用的信息,请告诉我,我会更新。

我知道,如果我不提供,nodemailer 会尝试创建它自己的 access_token,但在这里我为它提供了 access_tokenrefresh_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 身份验证选项中,我使用蛇形外壳而不是驼形外壳。多么愚蠢。

关于node.js - Nodemailer gmail oauth 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58986893/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com