我正在尝试在我的 Heroku NodeJS 应用程序中设置 SendGrid 附加组件。我创建了 API 密钥并将其设置为环境变量。
整个 API 密钥类似于:SG.actualValue.bbb_cccccc
我做的第一个设置是将整个密钥设置为我的 SENDGRID_API_KEY,但出现此错误:
API 密钥不以 SG 开头。
因此,我意识到错误并取消设置环境变量并将其再次设置为整个键的 actualValue 部分。
但是,我仍然遇到同样的错误。我尝试再次做同样的事情或重新启动终端(实际上是整个笔记本电脑)。
这是我尝试从 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);
我尝试创建一个新密钥并进行设置,但我得到了同样的错误。我尝试将它设置为整个密钥,但没有“.SG”或只是 bbb_ccccc 部分。先感谢您。
原文由 Cortoloman 发布,翻译遵循 CC BY-SA 4.0 许可协议
意味着 SendGrid 的 API 密钥 应该 以
SG.
开头,所以你没有正确设置环境变量。你需要检查一下。只需使用console.log
打印环境变量。或者,使用为您的应用程序启动控制台,并使用
printenv
打印环境变量。TIMES: 5
环境变量是通过 heroku config vars 设置的:例如
按预期收到电子邮件: