无法使用 Oauth2 通过 Google cloud 发送节点邮件程序(响应代码:530)



我已经配置了两个GMAIL帐户来集成nodemailerOauth2fromGoogle cloud当我第一次这样做时,我可以成功地从我的nodejs应用程序发送GMAIL的电子邮件。然而,今天我做了和以前一样的步骤,当我试图发送电子邮件时,它抛出了一个错误。

Error: Mail command failed: 530-5.7.0 Authentication Required

互联网上的一些答案说我需要在谷歌上激活不太安全的应用程序,但自2022年5月以来,这个选项被谷歌停用,所以我不能这样做。

在Google cloud上设置Oauth2的所有步骤(摘要):

  1. 创建新项目

  2. 已启用的服务和API

  3. OAuth同意屏幕上创建新的Oauth2

  4. 创建新的凭据,web应用程序,重定向:https://developers.google.com/oauthplayground

  5. 新增测试用户在OAuth同意界面

  6. oauthplayground选择"https://mail.google.com">服务并添加CLIENT_ID和CLIENT_SECRET,然后单击为令牌交换授权代码

这是相同的设置,我总是做当我需要配置nodejs与GMAIL,我应该工作,然后我复制所有必要的值到node.js CLIENT_ID, CLIENT_SECRET, refresh_tokens…


现在我想分别分享与nodemailer和googleapi相关的重要.js文件:

const nodemailer = require('nodemailer');
const urlUtil = require('../utils/urlUtil');
const config = require(urlUtil.getPath('../config.min.js'));
const googleApiUtil = require(urlUtil.getPath('../utils/googleApiUtil.min.js'));
/**
* Send generic email
* @param {*} destination to
* @param {*} subject title of the email
* @param {*} html html content for email template
* @returns promise
*/
function sendEmail(destination, subject, html) {
return new Promise((resolve) => {
googleApiUtil.getCredentials().then((credentials) => {
try {
let transporter = nodemailer.createTransport({
service: config.mail.service,
auth: {
type: 'OAuth2',
user: config.mail.user,
clientId: credentials.client_id,
clientSecret: credentials.client_secret,
refreshToken: credentials.refresh_token,
accessToken: credentials.accessToken
}, tls: {
rejectUnauthorized: false
}
});
let mailOptions = {};
mailOptions = {
from: `IARA <${config.mail.user}>`,
to: destination,
subject: subject,
html: html,
attachments: null
};
transporter.sendMail(mailOptions, resolve);
} catch (error) {
console.log(error);
}
});
});
}
//This should send an email to destinantion
sendEmail('adriel.kirch.1@gmail.com','teste','testando').then(r=> {
console.log(r)
})
/**
* Exports
*/
module.exports = {
sendEmail
};
const {google} = require('googleapis');
const urlUtil = require('../utils/urlUtil');
const config = require(urlUtil.getPath('../config.min.js'));
const fsUtil = require(urlUtil.getPath('../utils/fsUtil.min.js'));
//Basic credentials
const CLIENT_ID = '4123123..';
const CLIENT_SECRET = 'GO...';
//https://developers.google.com/oauthplayground
const REDIRECT_URI = 'https://developers.google.com/oauthplayground';
let refresh_token = '';
let accessToken = '';
if (!config.mail.refresh_token) {
refresh_token = '1//...';
} else {
refresh_token = config.mail.refresh_token;
}
const oAuth2Client = new google.auth.OAuth2(CLIENT_ID, CLIENT_SECRET, REDIRECT_URI);

oAuth2Client.setCredentials({
refresh_token: refresh_token,
});
oAuth2Client.refreshAccessToken((err, tokens) => {
console.log(tokens)
accessToken = tokens['access_token'];
refresh_token = tokens['refresh_token'];
//Set Config
if (err) {
console.log(err);
}
config.mail.accessToken = accessToken;
config.mail.refresh_token = refresh_token;
fsUtil.updateCredentials(config);
});
/**
* @returns Get credentials
*/
async function getCredentials() {
return {
redirect_uri: REDIRECT_URI,
refresh_token: refresh_token,
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
accessToken: accessToken
};
}

module.exports = {
getCredentials
};

结论:

我甚至不知道如果我的错误是在Oauth2配置或在Nodejs,我相信它是在谷歌云,但我不太确定,如果有人更有经验知道我如何解决它让我知道,谢谢很多

Fullstack跟踪:

Error: Mail command failed: 530-5.7.0 Authentication Required。欲知详情,请浏览530 5.7.0 https://support.google.com/mail/?p=WantAuthError dy43-20020a056870c7ab00b00101cdb417f1sm5627633oab。22 - GSMTP在SMTPConnection。_formatError (c:Javascript ProjectsIARA-backendnode_modulesnodemailerlibsmtp-connectionindex.js:784:19)在SMTPConnection。_actionMAIL (c:Javascript ProjectsIARA-backendnode_modulesnodemailerlibsmtp-connectionindex.js:1566:34)在SMTPConnection。Javascript项目(c: IARA-backend node_modules nodemailer lib smtp连接 index.js: 1041:18)在SMTPConnection。_processResponse (c:Javascript ProjectsIARA-backendnode_modulesnodemailerlibsmtp-connectionindex.js:947:20)在SMTPConnection。_onData (c:Javascript ProjectsIARA-backendnode_modulesnodemailerlibsmtp-connectionindex.js: 744:14)
at TLSSocket.SMTPConnection。_onSocketData (c:Javascript ProjectsIARA-backendnode_modulesnodemailerlibsmtp-connectionindex.js:189:44)在TLSSocket。发出(events.js 315:20):/streams/readable.js:309:12at readableAddChunk(内部/streams/readable.js:284:9)at TLSSocket.Readable.push (internal/streams/readable.js:223:10) {代码:"EENVELOPE",response: '530-5.7.0 Authentication Required. '在n' +了解更多信息'530 5.7.0 https://support.google.com/mail/?p=WantAuthError dy43-20020a056870c7ab00b00101cdb417f1sm5627633oab。22 - gsmtp',
responseCode: 530,命令:"邮件FROM"}

我刚刚解决了这个问题,这是我得到的最佳解决方案,每当我调用oAuth2Client.refreshAccessToken()函数时,我保存所有返回的凭据,例如refresh_token,access_token在配置文件(.json)上,所以当我调用发送电子邮件时,我从配置文件中更新了这些凭据。这其实很简单,抱歉在Stackoverflow上开了一个愚蠢的问题。

相关内容

最新更新