我正在尝试在我的 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 设置的:例如。
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
按预期收到电子邮件:
关于javascript - API key 不以 "SG."SendGrid 开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61863508/