【问题标题】:Sendgrid API key not reading correctly from env variableSendgrid API 密钥未从 env 变量中正确读取
【发布时间】:2020-10-02 18:44:29
【问题描述】:

我正在遵循 Node.js (https://app.sendgrid.com/guide/integrate/langs/nodejs) 上的 Sendgrid 设置指南,但我不断收到 API 密钥错误。

这是我的代码:

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'test@example.com',
  from: 'test@example.com',
  subject: 'Sending with Twilio SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
  html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};
sgMail
  .send(msg)
  .then(() => console.log('send mail success'))
  .catch(console.log);

我的 API 密钥已在我的 .env 文件中正确设置:

SENDGRID_API_KEY=SG.oqKbQHcNxxxxxxxxxxxxxkY5B4o

这是我收到的错误消息:

API key does not start with "SG.".
ResponseError: Unauthorized
    at BE-KeyCon/node_modules/@sendgrid/client/src/classes/client.js:133:29
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  code: 401,
  response: {
    headers: {
      server: 'nginx',
      date: 'Fri, 12 Jun 2020 21:31:08 GMT',
      'content-type': 'application/json',
      'content-length': '116',
      connection: 'close',
      'access-control-allow-origin': 'https://sendgrid.api-docs.io',
      'access-control-allow-methods': 'POST',
      'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
      'access-control-max-age': '600',
      'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html'
    },
    body: { errors: [Array] }
  }
}

如果我将 API 密钥设置为字符串,如下所示:

sgMail.setApiKey('SG.oqKbQHcNxxxxxxxxxxxxxkY5B4o')

然后就可以了。但显然出于安全原因,我不能这样离开。知道它为什么会这样以及如何解决它吗?

【问题讨论】:

  • process.env.SENDGRID_API_KEY 评估为什么?
  • 未定义!我尝试了所有环境变量,它们都返回未定义。这很奇怪...... .env 文件在我的根目录中,我需要 dotenv 库。
  • 如果我运行node -r dotenv/config util/sendgrid.js,那么它会正确评估为 api 密钥。
  • 您运行require('dotenv').config() 的时间有多早?您是否在致电process.env.SENDGRID_API_KEY 之前确保它已经发生?
  • 我刚刚运行了console.log(require('dotenv').config());,结果它找不到 .env 文件。我的项目中有两个主要文件夹,一个用于前端,一个用于后端,我在 BE 文件夹中有一个 .env 文件,认为这是我的根目录。我不得不将它移到实际的根目录,现在它工作正常。谢谢大家!

标签: node.js environment-variables sendgrid


【解决方案1】:

问题在于 .env 文件的位置。我的后端文件夹嵌套在另一个文件夹中,所以我不得不将它移到实际的根目录中。

【讨论】:

    【解决方案2】:

    从 npm 安装 dotenv 包并使用这段代码确保它获取 .env 文件:

    if (process.env.NODE_ENV !== 'production') require('dotenv').config();
    

    控制台登录process.env.SENDGRID_API_KEY以检查程序是否正在获取api密钥。

    【讨论】:

      【解决方案3】:

      曾经遇到过这个问题,但在我的情况下,简单地更新文件并没有完全解决。

      以下是我最终解决它的方法:
      在集成了 SendGrid API 的文件中包含 require('dotenv').config
      转到 Heroku 应用程序。单击“设置”页面并查找“配置变量”。 "Find 'Config Vars' in the 'Settings' page"

      按“显示配置”按钮并添加另一个密钥对。
      将“SENDGRID_API_KEY”设置为 KEY 并将您的 SG.xxxxxxYourAPIxxx 设置为值。 添加密钥对并重新运行应用程序。

      【讨论】:

        猜你喜欢
        • 2017-01-30
        • 2021-04-09
        • 2013-03-07
        • 1970-01-01
        • 2019-04-24
        • 1970-01-01
        • 2020-01-03
        • 2022-01-17
        • 2021-03-26
        相关资源
        最近更新 更多