sendgrid / sendgrid-php

The Official Twilio SendGrid PHP API Library
https://sendgrid.com
MIT License
1.49k stars 623 forks source link

"message":"Permission denied, wrong credentials","field":null,"help":null #831

Closed jtavarezdevops closed 4 years ago

jtavarezdevops commented 5 years ago

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:

thinkingserious commented 5 years ago

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

noxidsoft commented 5 years ago

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("no-reply@timelogger.info", "Example User");
$email->setSubject("Sending with SendGrid is Fun");
$email->addTo("demo@noxidsoft.com", "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.

iTrim commented 5 years ago

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? :)

mhamzas commented 5 years ago

Facing the same issue

devasia2112 commented 5 years ago

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.

mhamzas commented 5 years ago

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

iTrim commented 5 years ago

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

arazima commented 4 years ago

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

avtarnanrey commented 4 years ago

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.

kareemadesola commented 4 years ago

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.

childish-sambino commented 4 years ago

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

lukepassmore commented 4 years ago

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!

sloan8 commented 3 years ago

gettiing this issue when using the curl example

Wind010 commented 2 years ago

Ensure to include the Bearer prefix to the Authorization header: https://docs.sendgrid.com/for-developers/sending-email/curl-examples

curl --request POST \
  --url https://api.sendgrid.com/v3/mail/send \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"personalizations": [{"to": [{"email": "recipient@example.com"}]}],"from": {"email": "sendeexampexample@example.com"},"subject": "Hello, World!","content": [{"type": "text/plain", "value": "Heya!"}]}'