使用 api 发送邮件时,sparkpost 出现授权错误

问题描述

我正在尝试使用来自 sparkpost 的 api 从 netlify 函数发送电子邮件。但是当我使用 functions:invoke verify-email --no-identity 测试该功能时,我只收到 401 错误

我有这个简单的功能

0.7 * screen width

这是我得到的错误信息。我已经重新创建了 api-key 但结果相同

const SparkPost = require('sparkpost')
const client = new SparkPost(process.env.SPARKPOST)
const key = process.env.SPARKPOST
const handler = async(event,context,callback) => {
  console.log(key) //outputs the key,so this is not the problem
  return client.transmissions.send({
    options: {
      sandBox: true
    },content: {
      from: 'testing@sparkpostBox.com',subject: 'Hello,World!',html:'<html><body><p>Testing SparkPost - the world\'s most awesomest email service!</p></body></html>'
    },recipients: [
      {address: 'email@gmail.com'}
    ]
  })
  .then(data => {
    console.log('Woohoo! You just sent your first mailing!');
    console.log(data);
    return data
  })
  .catch(err => {
    console.log('Whoops! Something went wrong');
    console.log(err);
    return err
  });
}

解决方法

这里可能存在两个问题之一。

  1. 确保您的 API 密钥在此处启用了传输读/写https://app.sparkpost.com/account/api-keys

  2. 您在欧盟并尝试使用美国服务器。在这种情况下,您可以执行以下操作:

'host' => 'api.eu.sparkpost.com',

$httpClient = new GuzzleAdapter(new Client()); $sparky = 新 SparkPost($httpClient,['key'=>'**********','host' => 'api.eu.sparkpost.com']);

在此处查看一些详细信息https://github.com/SparkPost/php-sparkpost/issues/180

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...