InvalidClientTokenId:请求中包含的安全令牌无效 - 在 nestjs 中使用 AWS SES 服务发送电子邮件时出错

InvalidClientTokenId: The security token included in the request is invalid - Error getting while sending email using AWS SES service in nestjs

提问人:Happy Patel 提问时间:11/10/2023 更新时间:11/23/2023 访问量:25

问:

欢迎.ejs

<p>Hey <%= name %>,</p>
<p>
  Thanks for signing up for Nice App. We're very excited to have you on board.
</p>

<p>To get started using Nice App, please confirm your account below:</p>
<p>
  <a href="<%= confirmation_url %>">Confirm you account</a>
</p>

<p>
  If you're having trouble clicking the confirm account button, copy and paste
  the below URL into your web browser.
</p>

<a href="<%= confirmation_url %>"><%= confirmation_url %></a>

我在src/core/templates/中有这个电子邮件模板

email.service.ts

async sendEmailAWSSES(
    to: string,
    subject: string,
    template: string,
    data: object,
  ) {
    const config = {
      accessKeyId: 'XXXXXXXXXXXXXXXXXX',
      secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
      region: 'eu-west-1',
    };

    // Load the HTML template
    const emailTemplateSource = fs.readFileSync(
      `src/core/templates/${template}.ejs`,
      'utf-8',
    );
    const emailTemplate = Handlebars.compile(emailTemplateSource);

    // Compile the HTML template with data
    const html = emailTemplate(data);

    // Create sendEmail params
    const params = {
      Source: '[email protected]',
      Destination: {
        ToAddresses: [to],
      },
      Message: {
        Body: {
          Html: {
            Charset: 'UTF-8',
            Data: html,
          },
        },
        Subject: {
          Charset: 'UTF-8',
          Data: subject,
        },
      },
      ReplyToAddresses: [],
    };

    // Create the promise and SES service object
    const sendPromise = await new AWS.SES(config).sendEmail(params).promise();

    return sendPromise;
  }

我有这个功能,可以使用AWS SES服务发送电子邮件 在发送我收到的电子邮件时

InvalidClientTokenId:请求中包含的安全令牌是 无效

请注意,我使用的是 AWS SES 的正确凭证。

节点.js 亚马逊网络服务 电子邮件 nestjs 亚马逊

评论

0赞 Thomas 11/15/2023
你在本地运行吗?您最近是否更新了 AWS CLI 信条?
0赞 Happy Patel 11/20/2023
是的,我在本地运行这个。我只为 AWS SES 服务添加了 accessKeyId、secretAccessKey 和 region。这有什么遗漏吗?
0赞 Thomas 11/21/2023
尝试删除并运行~/.aws/credentialsaws configure
0赞 Happy Patel 11/23/2023
通过更改凭据解决的问题

答:

0赞 Happy Patel 11/23/2023 #1

我正在使用导致错误的过期凭据。