问题
SendGrid seems to be preventing my node js server sending emails.
I get this error message in the response on sending of an email:
"The provided authorization grant is invalid, expired or revoked"
I have an API key setup as well and have followed the documentation.
回答1:
You need to use the API KEY GENERATED
DO NOT USE the API KEY ID
Sendgrid only displays the generated key once when you create it.
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:
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...
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/34789622/sendgrid-api-key-not-working-the-provided-authorization-grant-is-invalid-expi