我正在尝试使用Nodemailer和OAuth2发送电子邮件。它在localhost上工作,但在Heroku上不工作,因此,我将OAuth添加到Nodemailer createTransport。但现在它甚至在localhost上也不工作。
我的代码是:
const oAuth2Client =new google.auth.OAuth2(process.env.CLIENT_ID,process.env.CLIENT_SECRET,process.env.REDIRECT_URI);
oAuth2Client.setCredentials({refresh_token:process.env.REFRESH_TOKEN});
const sendMail =async(email,code)=>{
try
{const accessToken = await oAuth2Client.getAccessToken();
let transport = nodemailer.createTransport(smtTransport({
service: "gmail",
auth: {
type:"OAuth2",
user: "kediaarts@gmail.com",
pass: process.env.EMAIL_PASS,
clientId:process.env.CLIENT_ID,
clientSecret:process.env.CLIENT_SECRET,
refreshToken:process.env.REFRESH_TOKEN,
accessToken:accessToken
}
}));
let mailOptions,link;
link = "http://localhost:5000"+email+"/"+code.toString() ;
mailOptions={
from:'"Horizons" <no-reply@gmail.com>',
to : email,
subject: "Please verify your Email",
html : "<h1>Horizons</h1><br><h2>Hi,<br> Please click on the link to verify your email.</h2><br><a href="+link+">Click here to verify</a>",
}
const result =await transport.sendMail(mailOptions);
return result;
} catch(error){
console.log(error);
}
}
出现以下错误:
Error: No refresh token or refresh handler callback is set.
at OAuth2Client.getAccessTokenAsync (E:\horizons_blog\node_modules\google-auth-library\build\src\auth\oauth2client.js:224:27)
at OAuth2Client.getAccessToken (E:\horizons_blog\node_modules\google-auth-library\build\src\auth\oauth2client.js:209:25)
at sendMail (file:///E:/horizons_blog/controllers/user.js:14:45)
at signUp (file:///E:/horizons_blog/controllers/user.js:78:9)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
1条答案
按热度按时间vom3gejh1#
问题是process.env.REFRESH_TOKEN没有被填充,主要是因为您还没有配置dotenv。
添加以下内容以填充它: