Sendgrid-php: "message":"Permission denied, wrong credentials","field":null,"help":null

Created on 5 May 2019  路  13Comments  路  Source: sendgrid/sendgrid-php

Keep getting 401 unauthorized error when trying to integrate sendgrid to a test email.

1.- I have generated a new API key in sendgrid.
2.-Env variable has been declared in the environment

401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Sun, 05 May 2019 14:01:18 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}

http://logicdatabase.com/sunshine/sendgrid-php/testemail.php

Issue Summary

A summary of the issue and the environment in which it occurs. If suitable, include the steps required to reproduce the bug. Please feel free to include screenshots, screencasts, code examples.

Steps to Reproduce

  1. This is the first step
  2. This is the second step
  3. Further steps, etc.

Any other information you want to share that is relevant to the issue being reported. Especially, why do you consider this to be a bug? What do you expect to happen instead?

Technical details:

  • sendgrid-php Version: master (latest commit: [commit number])
  • PHP Version: 5.6, 7.0, 7.1, 7.2 (as of May 17, 2018)
unknown or a waiting for feedback question

Most helpful comment

remove getenv -
$sendgrid = new \SendGrid('SENDGRID_API_KEY');

All 13 comments

Hello @jtavarezdevops,

Here are a couple of things to try:

  1. Try making your API call here. That will rule out the API Key itself.
  2. Print the value of the API Key just before you make the call to ensure it was set properly.

With Best Regards,

Elmer

I'm having a similar issue:

<?php
// curl -X "GET" "https://api.sendgrid.com/v3/templates" -H "Authorization: Bearer key.removed.bynox" -H "Content-Type: application/json";

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'GET https://api.sendgrid.com/v3/resource HTTP/1.1',
    'Authorization: Bearer key.removed.bynox'
));
curl_exec ($ch);

// require 'vendor/autoload.php'; // If you're using Composer (recommended)
// Comment out the above line if not using Composer
require("sendgrid/sendgrid-php.php");
// require("<PATH TO>/sendgrid-php.php");
// If not using Composer, uncomment the above line and
// download sendgrid-php.zip from the latest release here,
// replacing <PATH TO> with the path to the sendgrid-php.php file,
// which is included in the download:
// https://github.com/sendgrid/sendgrid-php/releases
$email = new \SendGrid\Mail\Mail();
$email->setFrom("[email protected]", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("[email protected]", "Example User test with sendgrid");
$email->addContent("text/plain", "and easy to do anywhere, even with PHP");
$email->addContent(
    "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
);
$sendgrid = new \SendGrid(getenv('key.removed.bynox'));
try {
    $response = $sendgrid->send($email);
    print $response->statusCode() . "\n";
    print_r($response->headers());
    print $response->body() . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '. $e->getMessage() ."\n";
}

curl_close ($ch);

Result (browser):

401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Mon, 30 Sep 2019 02:34:34 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}

Not using Composer. Downloaded latest from Github today.

401 Array ( [0] => HTTP/1.1 401 Unauthorized [1] => Server: nginx [2] => Date: Fri, 11 Oct 2019 17:27:49 GMT [3] => Content-Type: application/json [4] => Content-Length: 88 [5] => Connection: keep-alive [6] => Access-Control-Allow-Origin: https://sendgrid.api-docs.io [7] => Access-Control-Allow-Methods: POST [8] => Access-Control-Allow-Headers: Authorization, Content-Type, On-behalf-of, x-sg-elas-acl [9] => Access-Control-Max-Age: 600 [10] => X-No-CORS-Reason: https://sendgrid.com/docs/Classroom/Basics/API/cors.html [11] => [12] => ) {"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}

Any solution for this issue? :)

Facing the same issue

sounds like messy, no solution?

replace getenv('SENDGRID_API_KEY') from this line $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); with your API key and it should work.

sounds like messy, no solution?

replace getenv('SENDGRID_API_KEY') from this line $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); with your API key and it should work.

Thanks
replacing getenv function resolved for me but I am getting 202 in return but no email activity has been logged and not event I got any email

post code mhamzas that you are using leave out passwords etc..

remove getenv -
$sendgrid = new \SendGrid('SENDGRID_API_KEY');

sounds like messy, no solution?
replace getenv('SENDGRID_API_KEY') from this line $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); with your API key and it should work.

Thanks
replacing getenv function resolved for me but I am getting 202 in return but no email activity has been logged and not event I got any email

Errors List
As per the above link, it is not really an error. I followed the steps and got the same code. But email was sent almost immediately but it took few minutes for an activity entry to show up in my account.

But the key value will be exposed particularly when you want to deploy on heroku using gibhub. Is there a way to send emails using getenv('SENDGRID_API_KEY')? Please I desperately need an answer ASAP.

Loading the API key from an env var just one way of loading it. If you're having issues loading from the env var, check your environment details for how to properly configure env vars. For heroku for instance: https://devcenter.heroku.com/articles/config-vars

sounds like messy, no solution?

replace getenv('SENDGRID_API_KEY') from this line $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY')); with your API key and it should work.

+1 this worked for me, thanks!

gettiing this issue when using the curl example

Was this page helpful?
0 / 5 - 0 ratings

Related issues

atsareva picture atsareva  路  4Comments

FilipLukac picture FilipLukac  路  4Comments

moontrv picture moontrv  路  3Comments

morazain picture morazain  路  3Comments

elshafey picture elshafey  路  4Comments