I have written code for sending mail from my gmail account to another account by OAuth2. In OAuth2, we need a refreshToken and accessToken generated on https://developers.google.com/oauthplayground/ The accessToken generated by this will expires in 3600 seconds. I want some code that will generate accessToken.
I have written code where i direct put refreshToken and acessToken from this site https://developers.google.com/oauthplayground/ .
//javascript code main file app.js
async function sendEmail() {
const nodemailer = require("nodemailer");
const { google } = require("googleapis");
const OAuth2 = google.auth.OAuth2;
const smtpTransport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "***************@gmail.com",
clientId: "***********.apps.googleusercontent.com",
clientSecret: "*************",
refreshToken: "**************",
accessToken: "********************************"
}
});
const mailOptions = {
from: "**************@gmail.com",
to: "**************@gmail.com",
subject: "Hello",
generateTextFromHTML: true,
html: "<h1>TEST MAIL SAYS HELLO</h1>"
};
smtpTransport.sendMail(mailOptions, (error, response) => {
error ? console.log(error) : console.log(response);
smtpTransport.close();
});
}
sendEmail();
This is working fine but i want that accessToken generated by using some code.