sendgrid-nodejs: The provided authorization grant is invalid, expired, or revoked (401)

Issue Summary

When sending an email using the Nodejs V3 API, I receive the following error message:

{ 
    [SendGridError: Response error] message: 'Response error' } 
    { 
        statusCode: 401, 
        body: '{"errors":[{"message":"The provided authorization grant is invalid, expired, or revoked","field":null,"help":null}]}', 
        headers: { 
            server: 'nginx', 
            date: 'Wed, 31 Aug 2016 18:34:49 GMT', 
            'content-type': 'application/json', 
            'content-length': '116', 
            connection: 'close', 
            'x-frame-options': 'DENY'
        } 
    }
}

This message is produced on a localhost instance as in a live server environment.

Code snippet

The following code is used to send the email:

var helper = require('sendgrid').mail,
sg = require('sendgrid')(SENDGRID_API_KEY);

var from_email = new helper.Email('test123@sendgrid.com');
var to_email = new helper.Email(myOwnEmail);
var subject = 'Subject';
var content = new helper.Content('text/plain', 'test');
var mail = new helper.Mail(from_email, subject, to_email, content);

var request = sg.emptyRequest({
    method: 'POST',
    path: '/v3/mail/send',
    body: mail.toJSON()
});

sg.API(request, function(err, response) {
    console.log(err, response);
    if (!err) {
        res.send({
            message: 'An email has been sent to the provided email with further instructions.'
        });
    } else {
        return res.status(400).send({
            message: 'Failure sending email'
        });
    }
});

Technical details:

  • localhost: Windows 10 x64
  • liveserver: Heroku
  • sendgrid-nodejs Version: 4.0.2
  • npm version: 3.7.5
  • node version: 4.4.5

About this issue

  • Original URL
  • State: closed
  • Created 10 years ago
  • Comments: 18 (5 by maintainers)

Most upvoted comments

I created a new account with a new API KEY with the exact same code. Now I’m able to send emails while waiting for support takes forever.

Sorry for the trouble its working now i thought it was an error it was a 202 error . Sorry

if you need to send email via api with post methode you should do like this:

var helper= require(‘sendgrid’).mail;

router.post(‘/’ , function ( req , res) {

var sg=require('sendgrid')('your api key from your account');
var formEmail= new helper.Email('email@gmail.com');
var toEmail= new helper.Email(req.body.mail);
var subject='text';
var content = new helper.Content('text/plain', 'Your password has been successfully changed');

var mail = new helper.Mail(formEmail, subject, toEmail, content);

var request =sg.emptyRequest({
    method:'POST',
    path: '/v3/mail/send',
    body: mail.toJSON()
});

sg.API(request , function (error , response) {
    if(error){

        console.log('Error response not valid');

    }
});

})

module.exports=router;

Hi @ninox92,

This means there is an issue with your API Key.

First, please verify the value of SENGRID_API_KEY

Then please contact our support team at https://support.sendgrid.com so they can dig into what is wrong with your API Key’s permissions.

Thanks!

thank you it help me a lot

In the code you posted, you have SENGRID_API_KEY and I’m thinking you meant SENDGRID_API_KEY

SENGRID_API_KEY has been verified and tried a newly created API key but also reproduced the same error message.

Using a CURL POST request to send an email I though was able to successfully send an email using the same API key.

I’ll try to contact support once again.