34
votes

SendGrid seems to be preventing my node js server from sending emails.

I get this error message in the response on sending off an email:

The provided authorization grant is invalid, expired or revoked

I have an API key setup as well and have followed the documentation.

8
This would be easier to debug with some code ;) - kunal batra
Here's my code for this from my previous ticket: stackoverflow.com/questions/34788083/… - AngularM
Can you recheck your apikey, I just modified mine to be incorrect and got the issue "[Error: The provided authorization grant is invalid, expired, or revoked]". Double check that and let me know. - kunal batra
I've doubled checked and that seems fine - AngularM
@kunalbatra - any other ideas? - AngularM

8 Answers

72
votes

You need to use the API KEY GENERATED

enter image description here

DO NOT USE the API KEY ID

enter image description here

Sendgrid only displays the generated key once when you create it. enter image description here

If you didn't record it somewhere when you created the key you'll need to create a new key and then you'll probably want to delete the old key since it would be useless if you don't know what it is.

FYI: The API key in the screenshot above is already deleted. I deleted it right away so please don't worry about me leaking that key.

2
votes

This is a late answer and JAVA oriented.. But I simply copied the docs and didn't notice..

SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));

I just put my key in there and didn't see the getEnv. Silly of course.. but when trying to get things to work quickly...

2
votes

In Sendgrid v3, I had the similar issue when using env variable in Node JS. If I use env variable, I get the above issue. But if I drop the string into the require process, it works.

Doesn't Work:

SENDGRID_API_KEY=SG.XXXXXXXXXXXXXXXXXXX
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);

Works

var sg = require('sendgrid')('SG.XXXXXXXXXXXXXXXXXXX');

Replace SG.XXXXXXXXXXXXXXXXXXX with API Key Generated (which you can only see once during key generation).

EDIT

Note : Make sure you don't save this to public repository. If you do anyone can use your API Key and also your account will be suspended by Sendgrid team temporarily until you remove it from repository.

1
votes

For me I just had to generate a new API key. For some weird reason the former API key has become invalid, so I also added an alert for this case

1
votes
  • If you are using node js,
  • make sure you have the require('dotenv').config()line inside the file that needs the sendgrid/nodemailer module.
  • Without it, the sendgrid transporter will have an undefined value instead of the api_key.
1
votes

In my case I was trying to debug the connection by using telnet and kept getting rejected.

Turns out that these two are not equivalent, echo will include \n in the encoded string.

echo 'apikey' | base64
printf 'apikey' | base64

So yeah, make sure you don't include the newline in the API key.

0
votes

Here's my solution:

  1. Install the dotenv package: npm i dotenv

  2. Go to the earliest entry point of your application(i.e index.js) and put this at the top:

    const dotenv = require('dotenv').config();

  3. Create a .env file and add SENDGRID_API_KEY=<YOUR_API_KEY>. NO quotes '' or "".

  4. In your file which you use sendgrid, add this to the top:

    const sgMail = require('@sendgrid/mail');

    sgMail.setApiKey(process.env.SENDGRID_API_KEY);

Done.

-1
votes

I had the same issue, it disappeared after I verified my email address and enabled 2FA.