API 键不以 "SG." SendGrid 开头
API Key does not start with "SG." SendGrid
我正在尝试在我的 Heroku NodeJS 应用程序中设置 SendGrid 附加组件。
我创建了 API 密钥并将其设置为环境变量。
整个 API 键看起来像这样:SG.actualValue.bbb_cccccc
我做的第一个设置是将整个密钥设置为我的 SENDGRID_API_KEY,但出现此错误:
API key does not start with 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 部分。提前谢谢你。
API key does not start with SG.
表示SendGrid的API键应该以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=\[3[01;34m\]\w\[3[00m\] \[3[01;32m\]$ \[3[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
按预期收到电子邮件:
你好 如果你使用的是 node js,请确保你在需要 sendgrid/nodemailer 模块的文件中有 require('dotenv').config() 。没有它,sendgrid 传输器将有一个未定义的值而不是 api_key。
我也遇到了同样的问题,已经解决了。
我在 Node.js 上使用 SendGrids v3 和 dotenv v8.2
SendGrid 设置了一个 env 文件 SendGrid.env
,里面有导出 SENDGRID_API_KEY
,我将文件重命名为 .env
,删除了导出,现在可以使用了。
我的 sendEmail 文件的顶部如下所示:
require('dotenv').config();
const sgMail = require('@sendgrid/mail');
const apiKey = `${process.env.SENDGRID_API_KEY}`;
console.log("SendGrid key ", apiKey);
我的 .env
文件如下所示:
SENDGRID_API_KEY='SG.{left blank}................0EmA'
ANOTHER_API_KEY='ANOTEHERKEY'
您需要require('dotenv').config();
在您的测试文件的开头,否则它将无法找到您的密钥。
我遇到了同样的问题,我从 .env
文件中删除了 export
关键字,它对我有用
我在.env
中将单引号改为双引号。那修复了我的错误。这是我的 sendgrid.env
文件的样子:
SENDGRID_API_KEY = "SG.oDXKOMdlT3-nCEQt........"
我在开发环境中 运行 我的代码时遇到了这个问题。我也在使用 env-cmd 包。我通过更改我的 package.json 文件解决了这个问题:
"dev": "env-cmd -f ./config/dev.env nodemon src/index.js"
对此:
"dev": "env-cmd -f ./config/dev.env nodemon src/emails/account.js"
我正在尝试在我的 Heroku NodeJS 应用程序中设置 SendGrid 附加组件。 我创建了 API 密钥并将其设置为环境变量。
整个 API 键看起来像这样:SG.actualValue.bbb_cccccc
我做的第一个设置是将整个密钥设置为我的 SENDGRID_API_KEY,但出现此错误:
API key does not start with 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 部分。提前谢谢你。
API key does not start with SG.
表示SendGrid的API键应该以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=\[3[01;34m\]\w\[3[00m\] \[3[01;32m\]$ \[3[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
按预期收到电子邮件:
你好 如果你使用的是 node js,请确保你在需要 sendgrid/nodemailer 模块的文件中有 require('dotenv').config() 。没有它,sendgrid 传输器将有一个未定义的值而不是 api_key。 我也遇到了同样的问题,已经解决了。
我在 Node.js 上使用 SendGrids v3 和 dotenv v8.2
SendGrid 设置了一个 env 文件 SendGrid.env
,里面有导出 SENDGRID_API_KEY
,我将文件重命名为 .env
,删除了导出,现在可以使用了。
我的 sendEmail 文件的顶部如下所示:
require('dotenv').config();
const sgMail = require('@sendgrid/mail');
const apiKey = `${process.env.SENDGRID_API_KEY}`;
console.log("SendGrid key ", apiKey);
我的 .env
文件如下所示:
SENDGRID_API_KEY='SG.{left blank}................0EmA'
ANOTHER_API_KEY='ANOTEHERKEY'
您需要require('dotenv').config();
在您的测试文件的开头,否则它将无法找到您的密钥。
我遇到了同样的问题,我从 .env
文件中删除了 export
关键字,它对我有用
我在.env
中将单引号改为双引号。那修复了我的错误。这是我的 sendgrid.env
文件的样子:
SENDGRID_API_KEY = "SG.oDXKOMdlT3-nCEQt........"
我在开发环境中 运行 我的代码时遇到了这个问题。我也在使用 env-cmd 包。我通过更改我的 package.json 文件解决了这个问题:
"dev": "env-cmd -f ./config/dev.env nodemon src/index.js"
对此:
"dev": "env-cmd -f ./config/dev.env nodemon src/emails/account.js"