Nodemailerr,使用OAuth2的Gmail,访问令牌已过期

时间:2018-10-01 18:30:51

标签: node.js gmail google-oauth nodemailer

按照此处概述的步骤进行操作-Nodemailer/Gmail - What exactly is a refresh token and how do I get one?都可以正常工作,但是accessToken似乎一直在过期。我收到以下错误。

{ Error: Invalid status code 400
    at ClientRequest.req.on.res (/node_modules/nodemailer/lib/fetch/index.js:221:23)
    at emitOne (events.js:116:13)

代码-

    let transporter = nodeMailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true,
        auth: {
            type: 'OAuth2',
            user: '123@gmail.com',
            clientId: '123.apps.googleusercontent.com',
            clientSecret: '123',
            refreshToken: '123',
            accessToken: 'abc'
        }
    });

 let mailOptions = {
        from: 'from@gmail.com', // sender address
        to: 'send@gmail.com', // list of receivers
        subject: 'contact form', // Subject line
        text: '222', // plain text body
        html: '<p>Name:'+ req.body.name + '</p><p>Company: '+ req.body.company + '</p><p>Email: '+ req.body.email + '</p><p>Message: '+ req.body.message + '</p>'
    };

transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            res.status(400).json({
                status: "error",
                details: error
            })
        }
        res.status(200).json({
            status: "success"
        })
    });

如果我生成一个新的accessToken,那么一切都会按预期进行...我在做什么错?这不是理想的解决方案。

0 个答案:

没有答案