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

SHU*_*DGE 9 node.js firebase google-cloud-functions

我想在用户使用带有 firebase auth 的 Cloud-Function 登录时发送欢迎通知,所以我使用 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;
    }
Run Code Online (Sandbox Code Playgroud)

我参考这个代码 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)
Run Code Online (Sandbox Code Playgroud)

我还允许来自您的 Google 帐户的安全性较低的应用程序,并且还进行了两步验证 这里 但仍然有错误

我在 stackoverflow 中阅读了所有“类似问题”,我不知道我是否需要其他任何东西,或者我是否在做坏事

SHU*_*DGE 40

如果您在 Google 帐户上启用了 2 因素身份验证,则无法使用常规密码以编程方式访问 Gmail。您需要生成特定于应用程序的密码并使用该密码代替您的实际密码。

脚步:

登录您的 Google 帐户 转到我的帐户 > 登录和安全 > 应用密码(再次登录以确认是您本人) 向下滚动到选择应用(在密码和登录方法框中)并选择其他(自定义名称) ) 为这个应用程序密码命名,例如“nodemailer” 选择 Generate 复制生成的长密码并将其粘贴到您的 Node.js 脚本中,而不是您实际的 Gmail 密码。

  • 从此处添加应用程序密码:https://myaccount.google.com/apppasswords (8认同)
  • 如果您在“安全”-&gt;“登录 Google”面板下找不到“应用程序密码”,请在 https://myaccount.google.com/security 中看到两步验证设置为“开启” ?如果是,则访问 https://myaccount.google.com/apppasswords 应该允许您设置应用程序特定密码。 (3认同)

AMA*_*N N 12

为此,您需要使用应用程序密码。当您的 Gmail 帐户启用两步验证时,就会出现此问题。您可以使用应用程序密码绕过它。以下是如何生成应用程序密码。

  1. 选择 Gmail 右上角的个人资料图标,然后选择管理 Google 帐户。

  2. 选择左侧边栏中的安全性。

  3. 在登录 Google 部分下选择应用程序密码。然后系统会要求您确认 Gmail 登录凭据。

  4. 在“选择应用程序”下,选择“邮件”或“其他”(自定义名称),然后选择一个设备。

  5. 选择生成。

  6. 您的密码将出现在新窗口中。按照屏幕上的说明完成该过程,然后选择“完成”。

谷歌文档:https://support.google.com/mail/answer/185833? hl=en#zippy=%2Cwhy-you-may-need-an-app-password