NodeJS 错误:登录无效:需要特定于应用程序的密码

laximzn5  于 2023-01-16  发布在  Node.js
关注(0)|答案(6)|浏览(175)

我想在用户使用Cloud-Function和firebase身份验证登录时发送欢迎通知,因此我使用nodejs CLI并运行代码
我的index.js文件“使用严格”;

const functions = require('firebase-functions');
    const nodemailer = require('nodemailer');
    // Configure the email transport using the default SMTP transport and a GMail account.
    // For Gmail, enable these:
    // 1. https://www.google.com/settings/security/lesssecureapps
    // 2. https://accounts.google.com/DisplayUnlockCaptcha
    // For other types of transports such as Sendgrid see https://nodemailer.com/transports/
    // TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.

    const gmailEmail = functions.config().gmail.email;
    const gmailPassword = functions.config().gmail.password;
    const mailTransport = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: gmailEmail,
        pass: gmailPassword,
      },
    });

    // Your company name to include in the emails
    // TODO: Change this to your app or company name to customize the email sent.
    const APP_NAME = 'Cloud Storage for Firebase quickstart';

    // [START sendWelcomeEmail]
    /**
     * Sends a welcome email to new user.
     */
    // [START onCreateTrigger]
    exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
    // [END onCreateTrigger]
      // [START eventAttributes]
      const email = user.email; // The email of the user.
      const displayName = user.displayName; // The display name of the user.
      // [END eventAttributes]

      return sendWelcomeEmail(email, displayName);
    });
    // [END sendWelcomeEmail]

    // [START sendByeEmail]
    /**
     * Send an account deleted email confirmation to users who delete their accounts.
     */
    // [START onDeleteTrigger]
    exports.sendByeEmail = functions.auth.user().onDelete((user) => {
    // [END onDeleteTrigger]
      const email = user.email;
      const displayName = user.displayName;

      return sendGoodbyeEmail(email, displayName);
    });
    // [END sendByeEmail]

    // Sends a welcome email to the given user.
    async function sendWelcomeEmail(email, displayName) {
      const mailOptions = {
        from: `${APP_NAME} <noreply@firebase.com>`,
        to: email,
      };

      // The user subscribed to the newsletter.
      mailOptions.subject = `Welcome to ${APP_NAME}!`;
      mailOptions.text = `Hey ${displayName || ''}! Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
      await mailTransport.sendMail(mailOptions);
      console.log('New welcome email sent to:', email);
      return null;
    }

    // Sends a goodbye email to the given user.
    async function sendGoodbyeEmail(email, displayName) {
      const mailOptions = {
        from: `${APP_NAME} <noreply@firebase.com>`,
        to: email,
      };

      // The user unsubscribed to the newsletter.
      mailOptions.subject = `Bye!`;
      mailOptions.text = `Hey ${displayName || ''}!, We confirm that we have deleted your ${APP_NAME} account.`;
      await mailTransport.sendMail(mailOptions);
      console.log('Account deletion confirmation email sent to:', email);
      return null;
    }

我引用此代码https://github.com/firebase/functions-samples/blob/master/quickstarts/email-users/functions/index.js
但我运行代码后,我得到了错误

Error: Invalid login: 534-5.7.9 Application-specific password required. Learn more at
534 5.7.9  https://support.google.com/mail/?p=InvalidSecondFactor i82sm13686303ilf.32 - gsmtp
    at SMTPConnection._formatError (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:784:19)
    at SMTPConnection._actionAUTHComplete (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:1523:34)
    at SMTPConnection._responseActions.push.str (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:550:26)
    at SMTPConnection._processResponse (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:942:20)
    at SMTPConnection._onData (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:749:14)
    at TLSSocket.SMTPConnection._onSocketData.chunk (/srv/node_modules/nodemailer/lib/smtp-connection/index.js:195:44)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)

我也允许安全性较低的应用程序从您的谷歌帐户,也做了2步验证x1c 0d1x,但仍然得到一个错误
我在stackoverflow阅读了所有的“类似问题”,我不知道我是否还需要其他东西,或者我是否做了什么坏事

pgpifvop

pgpifvop1#

如果你在谷歌账户上启用了双重认证,你就不能使用普通密码以编程方式访问Gmail。你需要生成一个应用程序特定的密码,并使用它来代替你的实际密码。
步骤:
登录到您的Google帐户转到我的帐户〉登录和安全〉应用程序密码(再次登录以确认是您)向下滚动到选择应用程序(在密码和登录方法框中)并选择其他(自定义名称)为此应用程序密码命名,例如"nodemailer"选择生成复制生成的长密码并将其粘贴到您的Node.js脚本中,而不是您的实际Gmail密码。

vkc1a9a2

vkc1a9a22#

为此,您需要使用应用程序密码。当您的Gmail帐户启用两步验证时,会出现此问题。您可以使用应用程序密码绕过此问题。以下是如何生成应用程序密码。
1.选择Gmail右上角的个人资料图标,然后选择管理Google帐户。
1.在左侧栏中选择安全性。
1.在“登录Google”部分下选择应用程序密码,然后系统会要求您确认Gmail登录凭据。
1.在“选择应用程序”下,选取“邮件”或“其他(自定名称)”,然后选择一个设备。
1.选择生成。
1.您的密码将显示在新窗口中。按照屏幕上的说明完成此过程,然后选择“完成”。
谷歌文档:https://support.google.com/mail/answer/185833?hl=en#zippy=%2Cwhy-you-may-need-an-app-password

jyztefdp

jyztefdp4#

你不能在本地主机上做这个。我试过了。因为它不是一个安全的连接。

icomxhvb

icomxhvb5#

在Gmail中,启用两步验证。
然后创建应用程序密码并在SMTP上使用

6l7fqoea

6l7fqoea6#

我现在不能发表评论,但这里是如何解决
你不能在本地主机上做这个。我试过了。因为它不是一个安全的连接。
用于在您创建transporter时删除配置中secure: true

相关问题