Sendgrid API - 错误的用户名/密码错误

Sendgrid API - Bad username / password error

我正在尝试使用 Sendgrid 发送我的第一封电子邮件:

$sendgrid = new SendGrid('username', 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx');
$email = new SendGrid\Email();

$email->addTo("email1@gmail.com")
      ->setFrom("email@domain.com")
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");

这是我 运行 进入的错误:

PHP Fatal error: Uncaught exception 'SendGrid\Exception' with message '{"errors":["Bad username / password"],"message":"error"}'


而不是 'username' 和 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx' 我当然使用来自 Sendgrid 设置的真实 API 关键信息:

我使用了创建 API 密钥后提供的长字符串密钥。但是这一行似乎还是有问题: $sendgrid = new SendGrid('username', 'xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx');

我应该从哪里获取授权信息,以免出现错误的用户名/密码错误?

不要使用 'username',这是 API 密钥的名称,请尝试使用您的 SendGrid 用户名(您用于登录仪表板等的用户名)。

编辑:要使其正常工作,还要使用 Sendgrid 密码而不是 API 密钥。这对我有用。

编辑 2:虽然这解决了问题,但应该只使用所选答案中提到的 api 键。

另一个可能的答案是只将 api 键传递给 SendGrid() 构造函数;

$sendgrid = new SendGrid('xx.xxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxx');
$email = new SendGrid\Email();

$email->addTo("email1@gmail.com")
      ->setFrom("email@domain.com")
      ->setSubject("Sending with SendGrid is Fun")
      ->setHtml("and easy to do anywhere, even with PHP");