我正在尝试使用dotenv隐藏带有环境变量的客户端ID和客户端机密。这是错误的方法吗? 它不适用于OAuth2,我迷路了!
以下是我尝试使用环境变量的联系表中的一些代码:
const OAuth2 = google.auth.OAuth2;
require("dotenv").config();
const refresh_token = process.env.REFRESH_TOKEN;
const client_id = process.env.CLIENT_ID;
const client_secret = process.env.CLIENT_SECRET;
async function sendEmail() {
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
const oauth2Client = new OAuth2(
client_id, // Client id
client_secret, // Client Secret
"https://developers.google.com/oauthplayground" // Redirect URL
);
oauth2Client.setCredentials({
refresh_token
});
const tokens = await oauth2Client.refreshAccessToken();
const accessToken = tokens.credentials.access_token;
const smtpTransport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "csobr9@gmail.com",
client_id: client_id,
client_secret: client_secret,
refresh_token: refresh_token,
accessToken: accessToken
}
});
const smtpTransport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: "test@gmail.com",
client_id: client_id,
client_secret: client_secret,
refresh_token: refresh_token,
accessToken: accessToken
}
});
查看图片[错误消息] [1]
[1]: https://i.stack.imgur.com/RtruZ.png