微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自 Node.js 服务器应用程序的 SendGrid CORS 错误

如何解决来自 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?