如何解决来自 Node.js 服务器应用程序的 SendGrid CORS 错误
我只是想从 SendGrid 文档中获取一个简单的示例。这部分代码是从我的 Node.js + Express 应用程序中调用的:
function sendConfirmationEmail() {
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
to: 'my@email.com',// Change to your recipient
from: 'verifiedsender@gmail.com',// Change to your verified sender
subject: 'Hello,world!',text: 'Thank you for your interest. We will let you kNow if we need any more information.',html: '<strong>Thank you for your interest. We will let you kNow if we need any more information.</strong>',};
sgMail
.send(msg)
.then(() => {
console.log('Email sent')
})
.catch((error) => {
console.error(error)
});
}
看到这个记录:
> { Error: Forbidden
at axios.then.catch.error (node_modules/@sendgrid/client/src/classes/client.js:146:29)
at process._tickCallback (internal/process/next_tick.js:68:7)
code: 403,message: 'Forbidden',response:
{ headers:
{ server: 'Nginx',date: 'Sat,27 Feb 2021 15:58:48 GMT','content-type': 'application/json','content-length': '281',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] } } }
不确定为什么这是 CORS 错误,因为它不会发生在浏览器中。有什么想法吗?
来自 SendGrid 的建议解决方法没有帮助:
您可以创建一个基于服务器的应用程序,以保护您的 API 密钥不被公开。可以实现 NodeJS、PHP、Ruby、Python、C#、Go 和 Java 等语言,以从锁定的服务器环境的安全性调用 API。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。