Send bulk mail get error 401 Unauthorized
I'm using @sendgrid/mail module on Google Cloud Functions.
My mails are formatted like this:
{
to: user.email,
from: user.fromail,
cc: user.tl_email,
subject: 'Test',
html: setTemplate(),
attachments: [{
filename: 'test.png',
content: test,
content_id: 'test',
disposition: 'inline'
},
...]
};
I have an array of mails with different "to" and different html emails.
When I use sgMail.send(emailsArray) and user.email is just me, then the mails will be sent so the api key is working.
When I use sgMail.send(emailsArray) and user.email are different between them, I get this error:
{ Error: Unauthorized
at Request.http [as _callback] (node_modules/@sendgrid/client/src/classes/client.js:124:25)
at Request.self.callback (node_modules/request/request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (node_modules/request/request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
code: 401,
message: 'Unauthorized',
response:
{ headers:
{ server: 'nginx',
date: 'Fri, 16 Aug 2019 08:00:04 GMT',
'content-type': 'application/json',
'content-length': '74',
connection: 'close',
'access-control-allow-origin': 'https://sendgrid.api-docs.io',
'access-control-allow-methods': 'POST',
'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
'access-control-max-age': '600',
'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' },
body: undefined } }
Any help?
Technical details:
"@sendgrid/mail": "^6.4.0", Node.js Version: "^8.0.0"
More context: We've been using this package for almost two years. February 25 was the first time this ever happened for us. Then March 15 and March 17 and now twice today. This issue isn't resolved and seems to be getting worse.
@dockleryxk @crascher Sounds like the same issue: seemingly random 401s even though nothing else in your environment has changed. I highly doubt it's an issue with this library itself having reviewed the code and not spotted much complexity with regards to API keys.
My recommendation to open a support ticket still stands (support doesn't monitor issues here, engineers do). If you can provide them with details about when the request failed and any other details will help to track it down. If that doesn't help or you don't hear back in a timely manner, let me know. We're working to be more responsive in these forums.
Lastly, the library will soon be migrating the underlying HTTP client (https://github.com/sendgrid/sendgrid-nodejs/issues/1042) which we'll do a major version roll for. It's possible there was a bug in the current client so may be worth waiting for the changes to roll out to see if it resolves the issue.
@childish-sambino I'll open a ticket then, thank you! If I end up resolving it with them I will post back here about it
fwiw adding my comment here in case someone might hit it on different key words
We're running several dynamic template tests using jest. We started running into the same random 401 issue this afternoon, after running the same suite of tests all morning without issue. I reduced the test group to 10 tests, and we're running them in jest sequentially (so there's not multiple send requests being made at the same time). Of those 10 tests, no number of tests and no single test will fail on subsequent runs, reliably. Tests in the suite of 10 fail at random returning the same 401. We've gone so far as to log what the client is using for request data when using request internally - the Authorization header is correct for each failing request.
Given the simplistic nature of the client's code, this looks more like a network issue or network limitation to me.
the problem ist not solved yet; I am using latest version 7 and get sometimes:
Unauthorized (401) Error: Unauthorized at ResponseError (node_modules/@sendgrid/helpers/classes/response-error.js:19:5) at axios.then.catch.error (node_modules/@sendgrid/client/src/classes/client.js:105:29) at process._tickCallback (internal/process/next_tick.js:109:7)
code: 401, message: 'Unauthorized', response: { headers: { server: 'nginx', date: 'Fri, 24 Apr 2020 12:45:01 GMT', 'content-type': 'application/json', 'content-length': '74', connection: 'close', 'access-control-allow-origin': 'https://sendgrid.api-docs.io', 'access-control-allow-methods': 'POST', 'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl', 'access-control-max-age': '600', 'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' }, body: '' } }
Support says:
Mar 24, 12:33 PM PDT Hi,
Thanks for your reply and additional insight. I checked with our team regarding this issue. As it turns out, making rapid Mail Send calls in a row can intermittently lead to an odd 401 error. I sincerely apologize for any inconvenience caused. In this instance, we recommend implementing some form of retry logic within your code when you receive the 401 error as a workaround.
Hopefully, this information was helpful. Please let me know if I was unable to answer your questions, or if you have any other questions or concerns.
Best, ...
When does it get fixed and why the issue ist not open if it is still an issue?
It happened to me too, but i think my problem was different.
Because I had already reached limit to of 100 mail/day nothing else.
Bulk email is now sent perfectly without any errors.
idk if it helps just wanted to share.