javascript - API key 不以 "SG."SendGrid 开头

标签 javascript node.js email heroku sendgrid

我正在尝试在我的 Heroku NodeJS 应用程序中设置 SendGrid 插件。
我创建了 API key 并将其设置为环境变量。

整个 API key 看起来像:SG.actualValue.bbb_cccccc

我做的第一个设置我将整个 key 设置为我的 SENDGRID_API_KEY 并且我收到了这个错误:

API key does not start with SG.



所以,我意识到错误并取消设置环境变量并再次将其设置为整个键的实际值部分。

但是,我仍然遇到同样的错误。我尝试再次做同样的事情或重新启动终端(实际上是整个笔记本电脑)。

这是我试图从 SendGrid 设置页面运行的测试代码:
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);

我尝试创建一个新 key 并设置它,但我得到了同样的错误。我尝试将其设置为整个 key ,但没有“.SG”或只有 bbb_ccccc 部分。先感谢您。

最佳答案

API key does not start with SG.



表示 SendGrid 的 API key 应该 SG. 开始所以你没有正确设置环境变量。你需要检查一下。只需使用 console.log打印环境变量。或者,使用
$ heroku run bash -a mighty-river-12802

为您的应用程序启动控制台,并使用 printenv打印环境变量。
Running bash on ⬢ mighty-river-12802... up, run.1571 (Free)
~ $ printenv
TERM=xterm-256color
WEB_MEMORY=512
MEMORY_AVAILABLE=512
COLUMNS=367
DYNO=run.1571
PATH=/app/.heroku/node/bin:/app/.heroku/yarn/bin:/usr/local/bin:/usr/bin:/bin:/app/bin:/app/node_modules/.bin
WEB_CONCURRENCY=1
_=/usr/bin/printenv
PWD=/app
PS1=\[\033[01;34m\]\w\[\033[00m\] \[\033[01;32m\]$ \[\033[00m\]
NODE_ENV=production
LINES=49
TIMES=5
HOME=/app
SHLVL=2
PORT=6791
NODE_HOME=/app/.heroku/node
TIMES: 5环境变量是通过 heroku config vars 设置的:

enter image description here

例如。

const sgMail = require('@sendgrid/mail');

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'novaline.dulin@gmail.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);
$ export SENDGRID_API_KEY=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
$ node index.js
send mail success

按预期收到电子邮件:

enter image description here

关于javascript - API key 不以 "SG."SendGrid 开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61863508/

相关文章:

javascript - jQuery UI Datepicker 显示不正确的周数

javascript - Google 模型查看器 AR

pattern-matching - 如何识别电子邮件属于现有线程或对话

ruby-on-rails - Rails 应用程序的托管电子邮件

javascript - 更改链中的 css 属性 - JavaScript

javascript - 如何连接数组值作为键来获取 json 对象值

node.js - typescript 错误——错误 : Cannot find module 'typescript/tsc.js'

node.js - 使用restify时如何支持cors

javascript - 如何在 Node.js Express 中让我的 session 最后跨子域?

HTML 电子邮件缺少附件