Sendgrid:权限被拒绝,凭据错误

时间:2017-05-29 17:55:35

标签: php sendgrid

我使用sendgrid PHP发送电子邮件。这是我的代码:

require("sendgrid-php.php");

$from = new SendGrid\Email("Example User", "test@example.com");
$subject = "Sending with SendGrid is Fun";
$to = new SendGrid\Email("Example User", "to@gmail.com");
$content = new SendGrid\Content("text/plain", "and easy to do anywhere, even with PHP");
$mail = new SendGrid\Mail($from, $subject, $to, $content);

$apiKey = getenv('SG.key......');
$sg = new \SendGrid($apiKey);

$response = $sg->client->mail()->send()->post($mail);
echo $response->statusCode();
echo '<pre>';
print_r($response->headers());
echo '</pre>';
echo $response->body();

当我运行上面的代码时,它会显示以下错误消息:

  

401阵列(       [0] =&gt; HTTP / 1.1 401未经授权       [1] =&gt;服务器:nginx       [2] =&gt;日期:2017年5月29日星期一17:52:44 GMT       [3] =&gt; Content-Type:application / json       [4] =&gt;内容长度:88       [5] =&gt;连接:保持活力       [6] =&gt; X-Frame-Options:DENY       [7] =&gt; Access-Control-Allow-Origin:https://sendgrid.api-docs.io       [8] =&gt; Access-Control-Allow-Methods:POST       [9] =&gt; Access-Control-Allow-Headers:授权,内容类型,代表x-sg-elas-acl       [10] =&gt; Access-Control-Max-Age:600       [11] =&gt; X-No-CORS-原因:https://sendgrid.com/docs/Classroom/Basics/API/cors.html       [12] =&gt;       [13] =&gt; ){&#34;错误&#34;:[{&#34;消息&#34;:&#34;权限被拒绝,凭据错误&#34;&#34;字段&#34;:null,&#34;帮助&#34;:空}]}

我不明白为什么我会收到与权限相关的错误,因为我刚刚创建了API密钥。

1 个答案:

答案 0 :(得分:1)

这很常见。理解并做到这一点需要一个小时。您需要设置env或使用用户名密码验证系统。请阅读official document on Githubdocumentation heredocumentation here