【问题标题】:ResponseError: Unauthorized sendgridResponseError:未经授权的发送网格
【发布时间】:2023-01-19 14:39:54
【问题描述】:

我一直收到 401 未经授权,从昨天开始可以发送电子邮件,然后它开始给我错误

const sgMail = require('@sendgrid/mail')
const aikey = 'SG.GCxxxxxxxxxxxxw.vMxxxxxxxxxvxxxxxxxxxxxM'
sgMail.setApiKey(aikey)

const msg = {
  to: 'test@example.com', // Change to your recipient
  from: 'test@example.com', // Change to your verified sender
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
}

sgMail
  .send(msg)
  .then((response) => {
    console.log(response[0].statusCode)
    console.log(response[0].headers)
  })
  .catch((error) => {
    console.error(error)
  })

错误 401

ResponseError: Unauthorized
    at C:\Users\kennedy\Documents\try personalization\node_modules\@sendgrid\client\src\classes\client.js:146:29
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  code: 401,
  response: {
    headers: {
      server: 'nginx',
      date: 'Wed, 27 Jul 2022 08:22:58 GMT',
      'content-type': 'application/json',
      'content-length': '76',
      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',
      'strict-transport-security': 'max-age=600; includeSubDomains'
    },
    body: { errors: [Array] }
  }
}
[nodemon] clean exit - waiting for changes before restart

我正在使用 sendgrid 免费计划,这可能是原因吗?

【问题讨论】:

  • 在错误中,您可以看到 body: { errors: [Array] },如果您只记录 body,那么您将能够看到这些错误,它会告诉您哪里出了问题。

标签: node.js sendgrid


【解决方案1】:

您是否将这部分代码替换为“收件人”和“发件人”中的实际电子邮件 部分 ?

const msg = {
  to: 'test@example.com', // Change to your recipient
  from: 'test@example.com', // Change to your verified sender
  subject: 'Sending with SendGrid is Fun',
  text: 'and easy to do anywhere, even with Node.js',
}

【讨论】:

    猜你喜欢
    • 2018-06-15
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2011-04-21
    • 2016-09-11
    • 2018-10-09
    相关资源
    最近更新 更多